xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 2de44e65)
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 "OutputSections.h"
13 #include "SymbolTable.h"
14 #include "Target.h"
15 
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Support/FileOutputBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/StringSaver.h"
21 
22 using namespace llvm;
23 using namespace llvm::ELF;
24 using namespace llvm::object;
25 
26 using namespace lld;
27 using namespace lld::elf2;
28 
29 namespace {
30 // The writer writes a SymbolTable result to a file.
31 template <class ELFT> class Writer {
32 public:
33   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
34   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
35   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
36   typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
37   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
38   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
39   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
40   Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
41   void run();
42 
43 private:
44   void copyLocalSymbols();
45   void addReservedSymbols();
46   void createSections();
47   void addPredefinedSections();
48 
49   template <bool isRela>
50   void scanRelocs(InputSectionBase<ELFT> &C,
51                   iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels);
52 
53   void scanRelocs(InputSection<ELFT> &C);
54   void scanRelocs(InputSectionBase<ELFT> &S, const Elf_Shdr &RelSec);
55   void updateRelro(Elf_Phdr *Cur, Elf_Phdr *GnuRelroPhdr, uintX_t VA);
56   void assignAddresses();
57   void buildSectionMap();
58   void fixAbsoluteSymbols();
59   void openFile(StringRef OutputPath);
60   void writeHeader();
61   void writeSections();
62   bool isDiscarded(InputSectionBase<ELFT> *IS) const;
63   StringRef getOutputSectionName(StringRef S) const;
64   bool needsInterpSection() const {
65     return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
66   }
67   bool isOutputDynamic() const {
68     return !Symtab.getSharedFiles().empty() || Config->Shared;
69   }
70   int getPhdrsNum() const;
71 
72   OutputSection<ELFT> *getBss();
73   void addCommonSymbols(std::vector<DefinedCommon *> &Syms);
74   void addCopyRelSymbols(std::vector<SharedSymbol<ELFT> *> &Syms);
75 
76   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
77 
78   BumpPtrAllocator Alloc;
79   std::vector<OutputSectionBase<ELFT> *> OutputSections;
80   std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
81   unsigned getNumSections() const { return OutputSections.size() + 1; }
82 
83   void addRelIpltSymbols();
84   void addStartEndSymbols();
85   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
86   void setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags, uintX_t FileOff,
87                uintX_t VA, uintX_t Size, uintX_t Align);
88   void copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT> *From);
89 
90   bool HasRelro = false;
91   SymbolTable<ELFT> &Symtab;
92   std::vector<Elf_Phdr> Phdrs;
93 
94   uintX_t FileSize;
95   uintX_t SectionHeaderOff;
96 
97   llvm::StringMap<llvm::StringRef> InputToOutputSection;
98 };
99 } // anonymous namespace
100 
101 template <class ELFT> static bool shouldUseRela() { return ELFT::Is64Bits; }
102 
103 template <class ELFT> void elf2::writeResult(SymbolTable<ELFT> *Symtab) {
104   // Initialize output sections that are handled by Writer specially.
105   // Don't reorder because the order of initialization matters.
106   InterpSection<ELFT> Interp;
107   Out<ELFT>::Interp = &Interp;
108   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
109   Out<ELFT>::ShStrTab = &ShStrTab;
110   StringTableSection<ELFT> StrTab(".strtab", false);
111   if (!Config->StripAll)
112     Out<ELFT>::StrTab = &StrTab;
113   StringTableSection<ELFT> DynStrTab(".dynstr", true);
114   Out<ELFT>::DynStrTab = &DynStrTab;
115   GotSection<ELFT> Got;
116   Out<ELFT>::Got = &Got;
117   GotPltSection<ELFT> GotPlt;
118   if (Target->supportsLazyRelocations())
119     Out<ELFT>::GotPlt = &GotPlt;
120   PltSection<ELFT> Plt;
121   Out<ELFT>::Plt = &Plt;
122   std::unique_ptr<SymbolTableSection<ELFT>> SymTab;
123   if (!Config->StripAll) {
124     SymTab.reset(new SymbolTableSection<ELFT>(*Symtab, *Out<ELFT>::StrTab));
125     Out<ELFT>::SymTab = SymTab.get();
126   }
127   SymbolTableSection<ELFT> DynSymTab(*Symtab, *Out<ELFT>::DynStrTab);
128   Out<ELFT>::DynSymTab = &DynSymTab;
129   HashTableSection<ELFT> HashTab;
130   if (Config->SysvHash)
131     Out<ELFT>::HashTab = &HashTab;
132   GnuHashTableSection<ELFT> GnuHashTab;
133   if (Config->GnuHash)
134     Out<ELFT>::GnuHashTab = &GnuHashTab;
135   bool IsRela = shouldUseRela<ELFT>();
136   RelocationSection<ELFT> RelaDyn(IsRela ? ".rela.dyn" : ".rel.dyn", IsRela);
137   Out<ELFT>::RelaDyn = &RelaDyn;
138   RelocationSection<ELFT> RelaPlt(IsRela ? ".rela.plt" : ".rel.plt", IsRela);
139   if (Target->supportsLazyRelocations())
140     Out<ELFT>::RelaPlt = &RelaPlt;
141   DynamicSection<ELFT> Dynamic(*Symtab);
142   Out<ELFT>::Dynamic = &Dynamic;
143 
144   Writer<ELFT>(*Symtab).run();
145 }
146 
147 // The main function of the writer.
148 template <class ELFT> void Writer<ELFT>::run() {
149   buildSectionMap();
150   if (!Config->DiscardAll)
151     copyLocalSymbols();
152   addReservedSymbols();
153   createSections();
154   assignAddresses();
155   fixAbsoluteSymbols();
156   openFile(Config->OutputFile);
157   writeHeader();
158   writeSections();
159   error(Buffer->commit());
160 }
161 
162 namespace {
163 template <bool Is64Bits> struct SectionKey {
164   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
165   StringRef Name;
166   uint32_t Type;
167   uintX_t Flags;
168   uintX_t EntSize;
169 };
170 }
171 namespace llvm {
172 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
173   static SectionKey<Is64Bits> getEmptyKey() {
174     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0,
175                                 0};
176   }
177   static SectionKey<Is64Bits> getTombstoneKey() {
178     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
179                                 0, 0};
180   }
181   static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
182     return hash_combine(Val.Name, Val.Type, Val.Flags, Val.EntSize);
183   }
184   static bool isEqual(const SectionKey<Is64Bits> &LHS,
185                       const SectionKey<Is64Bits> &RHS) {
186     return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
187            LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
188            LHS.EntSize == RHS.EntSize;
189   }
190 };
191 }
192 
193 // The reason we have to do this early scan is as follows
194 // * To mmap the output file, we need to know the size
195 // * For that, we need to know how many dynamic relocs we will have.
196 // It might be possible to avoid this by outputting the file with write:
197 // * Write the allocated output sections, computing addresses.
198 // * Apply relocations, recording which ones require a dynamic reloc.
199 // * Write the dynamic relocations.
200 // * Write the rest of the file.
201 template <class ELFT>
202 template <bool isRela>
203 void Writer<ELFT>::scanRelocs(
204     InputSectionBase<ELFT> &C,
205     iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
206   typedef Elf_Rel_Impl<ELFT, isRela> RelType;
207   const ObjectFile<ELFT> &File = *C.getFile();
208   for (const RelType &RI : Rels) {
209     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
210     SymbolBody *Body = File.getSymbolBody(SymIndex);
211     uint32_t Type = RI.getType(Config->Mips64EL);
212 
213     if (Target->isGotRelative(Type))
214       HasGotOffRel = true;
215 
216     if (Target->isTlsLocalDynamicReloc(Type)) {
217       if (Target->isTlsOptimized(Type, nullptr))
218         continue;
219       if (Out<ELFT>::Got->addCurrentModuleTlsIndex())
220         Out<ELFT>::RelaDyn->addReloc({&C, &RI});
221       continue;
222     }
223 
224     // Set "used" bit for --as-needed.
225     if (Body && Body->isUndefined() && !Body->isWeak())
226       if (auto *S = dyn_cast<SharedSymbol<ELFT>>(Body->repl()))
227         S->File->IsUsed = true;
228 
229     if (Body)
230       Body = Body->repl();
231 
232     if (Body && Body->isTls() && Target->isTlsGlobalDynamicReloc(Type)) {
233       bool Opt = Target->isTlsOptimized(Type, Body);
234       if (!Opt && Out<ELFT>::Got->addDynTlsEntry(Body)) {
235         Out<ELFT>::RelaDyn->addReloc({&C, &RI});
236         Out<ELFT>::RelaDyn->addReloc({nullptr, nullptr});
237         Body->setUsedInDynamicReloc();
238         continue;
239       }
240       if (!canBePreempted(Body, true))
241         continue;
242     }
243 
244     if (Body && Body->isTls() && !Target->isTlsDynReloc(Type, *Body))
245       continue;
246 
247     if (Target->relocNeedsDynRelative(Type)) {
248       RelType *Rel = new (Alloc) RelType;
249       Rel->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
250       Rel->r_offset = RI.r_offset;
251       Out<ELFT>::RelaDyn->addReloc({&C, Rel});
252     }
253 
254     bool NeedsGot = false;
255     bool NeedsPlt = false;
256     if (Body) {
257       if (auto *E = dyn_cast<SharedSymbol<ELFT>>(Body)) {
258         if (E->NeedsCopy)
259           continue;
260         if (Target->needsCopyRel(Type, *Body))
261           E->NeedsCopy = true;
262       }
263       NeedsPlt = Target->relocNeedsPlt(Type, *Body);
264       if (NeedsPlt) {
265         if (Body->isInPlt())
266           continue;
267         Out<ELFT>::Plt->addEntry(Body);
268       }
269       NeedsGot = Target->relocNeedsGot(Type, *Body);
270       if (NeedsGot) {
271         if (NeedsPlt && Target->supportsLazyRelocations()) {
272           Out<ELFT>::GotPlt->addEntry(Body);
273         } else {
274           if (Body->isInGot())
275             continue;
276           Out<ELFT>::Got->addEntry(Body);
277         }
278       }
279     }
280 
281     // An STT_GNU_IFUNC symbol always uses a PLT entry, and all references
282     // to the symbol go through the PLT. This is true even for a local
283     // symbol, although local symbols normally do not require PLT entries.
284     if (Body && isGnuIFunc<ELFT>(*Body)) {
285       Body->setUsedInDynamicReloc();
286       Out<ELFT>::RelaPlt->addReloc({&C, &RI});
287       continue;
288     }
289 
290     if (Config->EMachine == EM_MIPS) {
291       if (NeedsGot) {
292         // MIPS ABI has special rules to process GOT entries
293         // and doesn't require relocation entries for them.
294         // See "Global Offset Table" in Chapter 5 in the following document
295         // for detailed description:
296         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
297         Body->setUsedInDynamicReloc();
298         continue;
299       }
300       if (Body == Config->MipsGpDisp)
301         // MIPS _gp_disp designates offset between start of function and gp
302         // pointer into GOT therefore any relocations against it do not require
303         // dynamic relocation.
304         continue;
305     }
306 
307     // Here we are creating a relocation for the dynamic linker based on
308     // a relocation from an object file, but some relocations need no
309     // load-time fixup. Skip such relocation.
310     bool CBP = canBePreempted(Body, NeedsGot);
311     bool NoDynrel = Target->isRelRelative(Type) || Target->isSizeReloc(Type);
312     if (!CBP && (NoDynrel || !Config->Shared))
313       continue;
314 
315     if (CBP)
316       Body->setUsedInDynamicReloc();
317     if (NeedsPlt && Target->supportsLazyRelocations())
318       Out<ELFT>::RelaPlt->addReloc({&C, &RI});
319     else
320       Out<ELFT>::RelaDyn->addReloc({&C, &RI});
321   }
322 }
323 
324 template <class ELFT> void Writer<ELFT>::scanRelocs(InputSection<ELFT> &C) {
325   if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
326     return;
327 
328   for (const Elf_Shdr *RelSec : C.RelocSections)
329     scanRelocs(C, *RelSec);
330 }
331 
332 template <class ELFT>
333 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &S,
334                               const Elf_Shdr &RelSec) {
335   ELFFile<ELFT> &EObj = S.getFile()->getObj();
336   if (RelSec.sh_type == SHT_RELA)
337     scanRelocs(S, EObj.relas(&RelSec));
338   else
339     scanRelocs(S, EObj.rels(&RelSec));
340 }
341 
342 template <class ELFT>
343 static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) {
344   if (Config->Shared && !Config->NoUndefined)
345     return;
346 
347   std::string Msg = "undefined symbol: " + Sym->getName().str();
348   if (ELFFileBase<ELFT> *File = Symtab.findFile(Sym))
349     Msg += " in " + File->getName().str();
350   if (Config->NoInhibitExec)
351     warning(Msg);
352   else
353     error(Msg);
354 }
355 
356 // Local symbols are not in the linker's symbol table. This function scans
357 // each object file's symbol table to copy local symbols to the output.
358 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
359   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
360     for (const Elf_Sym &Sym : F->getLocalSymbols()) {
361       ErrorOr<StringRef> SymNameOrErr = Sym.getName(F->getStringTable());
362       error(SymNameOrErr);
363       StringRef SymName = *SymNameOrErr;
364       if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym))
365         continue;
366       if (Out<ELFT>::SymTab)
367         Out<ELFT>::SymTab->addLocalSymbol(SymName);
368     }
369   }
370 }
371 
372 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
373 // we would like to make sure appear is a specific order to maximize their
374 // coverage by a single signed 16-bit offset from the TOC base pointer.
375 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
376 // sections. This will put it next to the loaded special PPC64 sections (and,
377 // thus, within reach of the TOC base pointer).
378 static int getPPC64SectionRank(StringRef SectionName) {
379   return StringSwitch<int>(SectionName)
380            .Case(".tocbss", 0)
381            .Case(".branch_lt", 2)
382            .Case(".toc", 3)
383            .Case(".toc1", 4)
384            .Case(".opd", 5)
385            .Default(1);
386 }
387 
388 template <class ELFT> static bool isRelroSection(OutputSectionBase<ELFT> *Sec) {
389   typename OutputSectionBase<ELFT>::uintX_t Flags = Sec->getFlags();
390   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
391     return false;
392   if (Flags & SHF_TLS)
393     return true;
394   uint32_t Type = Sec->getType();
395   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
396       Type == SHT_PREINIT_ARRAY)
397     return true;
398   if (Sec == Out<ELFT>::GotPlt)
399     return Config->ZNow;
400   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
401     return true;
402   StringRef S = Sec->getName();
403   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
404          S == ".eh_frame";
405 }
406 
407 // Output section ordering is determined by this function.
408 template <class ELFT>
409 static bool compareOutputSections(OutputSectionBase<ELFT> *A,
410                                   OutputSectionBase<ELFT> *B) {
411   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
412 
413   uintX_t AFlags = A->getFlags();
414   uintX_t BFlags = B->getFlags();
415 
416   // Allocatable sections go first to reduce the total PT_LOAD size and
417   // so debug info doesn't change addresses in actual code.
418   bool AIsAlloc = AFlags & SHF_ALLOC;
419   bool BIsAlloc = BFlags & SHF_ALLOC;
420   if (AIsAlloc != BIsAlloc)
421     return AIsAlloc;
422 
423   // We don't have any special requirements for the relative order of
424   // two non allocatable sections.
425   if (!AIsAlloc)
426     return false;
427 
428   // We want the read only sections first so that they go in the PT_LOAD
429   // covering the program headers at the start of the file.
430   bool AIsWritable = AFlags & SHF_WRITE;
431   bool BIsWritable = BFlags & SHF_WRITE;
432   if (AIsWritable != BIsWritable)
433     return BIsWritable;
434 
435   // For a corresponding reason, put non exec sections first (the program
436   // header PT_LOAD is not executable).
437   bool AIsExec = AFlags & SHF_EXECINSTR;
438   bool BIsExec = BFlags & SHF_EXECINSTR;
439   if (AIsExec != BIsExec)
440     return BIsExec;
441 
442   // If we got here we know that both A and B are in the same PT_LOAD.
443 
444   // The TLS initialization block needs to be a single contiguous block in a R/W
445   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
446   // sections are placed here as they don't take up virtual address space in the
447   // PT_LOAD.
448   bool AIsTls = AFlags & SHF_TLS;
449   bool BIsTls = BFlags & SHF_TLS;
450   if (AIsTls != BIsTls)
451     return AIsTls;
452 
453   // The next requirement we have is to put nobits sections last. The
454   // reason is that the only thing the dynamic linker will see about
455   // them is a p_memsz that is larger than p_filesz. Seeing that it
456   // zeros the end of the PT_LOAD, so that has to correspond to the
457   // nobits sections.
458   bool AIsNoBits = A->getType() == SHT_NOBITS;
459   bool BIsNoBits = B->getType() == SHT_NOBITS;
460   if (AIsNoBits != BIsNoBits)
461     return BIsNoBits;
462 
463   // We place RelRo section before plain r/w ones.
464   bool AIsRelRo = isRelroSection(A);
465   bool BIsRelRo = isRelroSection(B);
466   if (AIsRelRo != BIsRelRo)
467     return AIsRelRo;
468 
469   // Some architectures have additional ordering restrictions for sections
470   // within the same PT_LOAD.
471   if (Config->EMachine == EM_PPC64)
472     return getPPC64SectionRank(A->getName()) <
473            getPPC64SectionRank(B->getName());
474 
475   return false;
476 }
477 
478 template <class ELFT> OutputSection<ELFT> *Writer<ELFT>::getBss() {
479   if (!Out<ELFT>::Bss) {
480     Out<ELFT>::Bss =
481         new OutputSection<ELFT>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
482     OwningSections.emplace_back(Out<ELFT>::Bss);
483     OutputSections.push_back(Out<ELFT>::Bss);
484   }
485   return Out<ELFT>::Bss;
486 }
487 
488 // Until this function is called, common symbols do not belong to any section.
489 // This function adds them to end of BSS section.
490 template <class ELFT>
491 void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
492   if (Syms.empty())
493     return;
494 
495   // Sort the common symbols by alignment as an heuristic to pack them better.
496   std::stable_sort(Syms.begin(), Syms.end(),
497                    [](const DefinedCommon *A, const DefinedCommon *B) {
498                      return A->MaxAlignment > B->MaxAlignment;
499                    });
500 
501   uintX_t Off = getBss()->getSize();
502   for (DefinedCommon *C : Syms) {
503     Off = align(Off, C->MaxAlignment);
504     C->OffsetInBss = Off;
505     Off += C->Size;
506   }
507 
508   Out<ELFT>::Bss->setSize(Off);
509 }
510 
511 // Reserve space in .bss for copy relocations.
512 template <class ELFT>
513 void Writer<ELFT>::addCopyRelSymbols(std::vector<SharedSymbol<ELFT> *> &Syms) {
514   if (Syms.empty())
515     return;
516   uintX_t Off = getBss()->getSize();
517   for (SharedSymbol<ELFT> *C : Syms) {
518     const Elf_Sym &Sym = C->Sym;
519     const Elf_Shdr *Sec = C->File->getSection(Sym);
520     uintX_t SecAlign = Sec->sh_addralign;
521     unsigned TrailingZeros =
522         std::min(countTrailingZeros(SecAlign),
523                  countTrailingZeros((uintX_t)Sym.st_value));
524     uintX_t Align = 1 << TrailingZeros;
525     Out<ELFT>::Bss->updateAlign(Align);
526     Off = align(Off, Align);
527     C->OffsetInBss = Off;
528     Off += Sym.st_size;
529   }
530   Out<ELFT>::Bss->setSize(Off);
531 }
532 
533 template <class ELFT>
534 StringRef Writer<ELFT>::getOutputSectionName(StringRef S) const {
535   auto It = InputToOutputSection.find(S);
536   if (It != std::end(InputToOutputSection))
537     return It->second;
538 
539   if (S.startswith(".text."))
540     return ".text";
541   if (S.startswith(".rodata."))
542     return ".rodata";
543   if (S.startswith(".data.rel.ro"))
544     return ".data.rel.ro";
545   if (S.startswith(".data."))
546     return ".data";
547   if (S.startswith(".bss."))
548     return ".bss";
549   return S;
550 }
551 
552 template <class ELFT>
553 void reportDiscarded(InputSectionBase<ELFT> *IS,
554                      const std::unique_ptr<ObjectFile<ELFT>> &File) {
555   if (!Config->PrintGcSections || !IS || IS->isLive())
556     return;
557   llvm::errs() << "removing unused section from '" << IS->getSectionName()
558                << "' in file '" << File->getName() << "'\n";
559 }
560 
561 template <class ELFT>
562 bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *IS) const {
563   if (!IS || !IS->isLive() || IS == &InputSection<ELFT>::Discarded)
564     return true;
565   return InputToOutputSection.lookup(IS->getSectionName()) == "/DISCARD/";
566 }
567 
568 template <class ELFT>
569 static bool compareSections(OutputSectionBase<ELFT> *A,
570                             OutputSectionBase<ELFT> *B) {
571   auto ItA = Config->OutputSections.find(A->getName());
572   auto ItEnd = std::end(Config->OutputSections);
573   if (ItA == ItEnd)
574     return compareOutputSections(A, B);
575   auto ItB = Config->OutputSections.find(B->getName());
576   if (ItB == ItEnd)
577     return compareOutputSections(A, B);
578 
579   return std::distance(ItA, ItB) > 0;
580 }
581 
582 // The beginning and the ending of .rel[a].plt section are marked
583 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
584 // executable. The runtime needs these symbols in order to resolve
585 // all IRELATIVE relocs on startup. For dynamic executables, we don't
586 // need these symbols, since IRELATIVE relocs are resolved through GOT
587 // and PLT. For details, see http://www.airs.com/blog/archives/403.
588 template <class ELFT>
589 void Writer<ELFT>::addRelIpltSymbols() {
590   if (isOutputDynamic() || !Out<ELFT>::RelaPlt)
591     return;
592   bool IsRela = shouldUseRela<ELFT>();
593 
594   StringRef S = IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
595   if (Symtab.find(S))
596     Symtab.addAbsolute(S, ElfSym<ELFT>::RelaIpltStart);
597 
598   S = IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
599   if (Symtab.find(S))
600     Symtab.addAbsolute(S, ElfSym<ELFT>::RelaIpltEnd);
601 }
602 
603 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
604   if (!B.isUsedInRegularObj())
605     return false;
606 
607   // Don't include synthetic symbols like __init_array_start in every output.
608   if (auto *U = dyn_cast<DefinedRegular<ELFT>>(&B))
609     if (&U->Sym == &ElfSym<ELFT>::IgnoredWeak ||
610         &U->Sym == &ElfSym<ELFT>::Ignored)
611       return false;
612 
613   return true;
614 }
615 
616 static bool includeInDynamicSymtab(const SymbolBody &B) {
617   uint8_t V = B.getVisibility();
618   if (V != STV_DEFAULT && V != STV_PROTECTED)
619     return false;
620   if (Config->ExportDynamic || Config->Shared)
621     return true;
622   return B.isUsedInDynamicReloc();
623 }
624 
625 // This class knows how to create an output section for a given
626 // input section. Output section type is determined by various
627 // factors, including input section's sh_flags, sh_type and
628 // linker scripts.
629 namespace {
630 template <class ELFT> class OutputSectionFactory {
631   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
632   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
633 
634 public:
635   std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
636                                                     StringRef OutsecName);
637 
638   OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type, uintX_t Flags);
639 
640 private:
641   SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
642                                        StringRef OutsecName);
643 
644   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map;
645 };
646 }
647 
648 template <class ELFT>
649 std::pair<OutputSectionBase<ELFT> *, bool>
650 OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
651                                    StringRef OutsecName) {
652   SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
653   OutputSectionBase<ELFT> *&Sec = Map[Key];
654   if (Sec)
655     return {Sec, false};
656 
657   switch (C->SectionKind) {
658   case InputSectionBase<ELFT>::Regular:
659     Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
660     break;
661   case InputSectionBase<ELFT>::EHFrame:
662     Sec = new EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
663     break;
664   case InputSectionBase<ELFT>::Merge:
665     Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
666     break;
667   case InputSectionBase<ELFT>::MipsReginfo:
668     Sec = new MipsReginfoOutputSection<ELFT>();
669     break;
670   }
671   return {Sec, true};
672 }
673 
674 template <class ELFT>
675 OutputSectionBase<ELFT> *OutputSectionFactory<ELFT>::lookup(StringRef Name,
676                                                             uint32_t Type,
677                                                             uintX_t Flags) {
678   return Map.lookup({Name, Type, Flags, 0});
679 }
680 
681 template <class ELFT>
682 SectionKey<ELFT::Is64Bits>
683 OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C,
684                                       StringRef OutsecName) {
685   const Elf_Shdr *H = C->getSectionHdr();
686   uintX_t Flags = H->sh_flags & ~SHF_GROUP;
687 
688   // For SHF_MERGE we create different output sections for each sh_entsize.
689   // This makes each output section simple and keeps a single level
690   // mapping from input to output.
691   uintX_t EntSize = isa<MergeInputSection<ELFT>>(C) ? H->sh_entsize : 0;
692 
693   // GNU as can give .eh_frame secion type SHT_PROGBITS or SHT_X86_64_UNWIND
694   // depending on the construct. We want to canonicalize it so that
695   // there is only one .eh_frame in the end.
696   uint32_t Type = H->sh_type;
697   if (Type == SHT_PROGBITS && Config->EMachine == EM_X86_64 &&
698       isa<EHInputSection<ELFT>>(C))
699     Type = SHT_X86_64_UNWIND;
700 
701   return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, EntSize};
702 }
703 
704 // The linker is expected to define some symbols depending on
705 // the linking result. This function defines such symbols.
706 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
707   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
708   // static linking the linker is required to optimize away any references to
709   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
710   // to avoid the undefined symbol error.
711   if (!isOutputDynamic())
712     Symtab.addIgnored("__tls_get_addr");
713 
714   // If the "_end" symbol is referenced, it is expected to point to the address
715   // right after the data segment. Usually, this symbol points to the end
716   // of .bss section or to the end of .data section if .bss section is absent.
717   // The order of the sections can be affected by linker script,
718   // so it is hard to predict which section will be the last one.
719   // So, if this symbol is referenced, we just add the placeholder here
720   // and update its value later.
721   if (Symtab.find("_end"))
722     Symtab.addAbsolute("_end", ElfSym<ELFT>::End);
723 
724   // If there is an undefined symbol "end", we should initialize it
725   // with the same value as "_end". In any other case it should stay intact,
726   // because it is an allowable name for a user symbol.
727   if (SymbolBody *B = Symtab.find("end"))
728     if (B->isUndefined())
729       Symtab.addAbsolute("end", ElfSym<ELFT>::End);
730 }
731 
732 // Create output section objects and add them to OutputSections.
733 template <class ELFT> void Writer<ELFT>::createSections() {
734   // Add .interp first because some loaders want to see that section
735   // on the first page of the executable file when loaded into memory.
736   if (needsInterpSection())
737     OutputSections.push_back(Out<ELFT>::Interp);
738 
739   // Create output sections for input object file sections.
740   std::vector<OutputSectionBase<ELFT> *> RegularSections;
741   OutputSectionFactory<ELFT> Factory;
742   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
743     for (InputSectionBase<ELFT> *C : F->getSections()) {
744       if (isDiscarded(C)) {
745         reportDiscarded(C, F);
746         continue;
747       }
748       OutputSectionBase<ELFT> *Sec;
749       bool IsNew;
750       std::tie(Sec, IsNew) =
751           Factory.create(C, getOutputSectionName(C->getSectionName()));
752       if (IsNew) {
753         OwningSections.emplace_back(Sec);
754         OutputSections.push_back(Sec);
755         RegularSections.push_back(Sec);
756       }
757       Sec->addSection(C);
758     }
759   }
760 
761   Out<ELFT>::Bss = static_cast<OutputSection<ELFT> *>(
762       Factory.lookup(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE));
763 
764   // If we have a .opd section (used under PPC64 for function descriptors),
765   // store a pointer to it here so that we can use it later when processing
766   // relocations.
767   Out<ELFT>::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC);
768 
769   Out<ELFT>::Dynamic->PreInitArraySec = Factory.lookup(
770       ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC);
771   Out<ELFT>::Dynamic->InitArraySec =
772       Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC);
773   Out<ELFT>::Dynamic->FiniArraySec =
774       Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC);
775 
776   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
777   // symbols for sections, so that the runtime can get the start and end
778   // addresses of each section by section name. Add such symbols.
779   addStartEndSymbols();
780   for (OutputSectionBase<ELFT> *Sec : RegularSections)
781     addStartStopSymbols(Sec);
782 
783   // Scan relocations. This must be done after every symbol is declared so that
784   // we can correctly decide if a dynamic relocation is needed.
785   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
786     for (InputSectionBase<ELFT> *C : F->getSections()) {
787       if (isDiscarded(C))
788         continue;
789       if (auto *S = dyn_cast<InputSection<ELFT>>(C))
790         scanRelocs(*S);
791       else if (auto *S = dyn_cast<EHInputSection<ELFT>>(C))
792         if (S->RelocSection)
793           scanRelocs(*S, *S->RelocSection);
794     }
795   }
796 
797   // Define __rel[a]_iplt_{start,end} symbols if needed.
798   addRelIpltSymbols();
799 
800   // Now that we have defined all possible symbols including linker-
801   // synthesized ones. Visit all symbols to give the finishing touches.
802   std::vector<DefinedCommon *> CommonSymbols;
803   std::vector<SharedSymbol<ELFT> *> CopyRelSymbols;
804   for (auto &P : Symtab.getSymbols()) {
805     SymbolBody *Body = P.second->Body;
806     if (auto *U = dyn_cast<Undefined>(Body))
807       if (!U->isWeak() && !U->canKeepUndefined())
808         reportUndefined<ELFT>(Symtab, Body);
809 
810     if (auto *C = dyn_cast<DefinedCommon>(Body))
811       CommonSymbols.push_back(C);
812     if (auto *SC = dyn_cast<SharedSymbol<ELFT>>(Body))
813       if (SC->NeedsCopy)
814         CopyRelSymbols.push_back(SC);
815 
816     if (!includeInSymtab<ELFT>(*Body))
817       continue;
818     if (Out<ELFT>::SymTab)
819       Out<ELFT>::SymTab->addSymbol(Body);
820 
821     if (isOutputDynamic() && includeInDynamicSymtab(*Body))
822       Out<ELFT>::DynSymTab->addSymbol(Body);
823   }
824   addCommonSymbols(CommonSymbols);
825   addCopyRelSymbols(CopyRelSymbols);
826 
827   // So far we have added sections from input object files.
828   // This function adds linker-created Out<ELFT>::* sections.
829   addPredefinedSections();
830 
831   std::stable_sort(OutputSections.begin(), OutputSections.end(),
832                    compareSections<ELFT>);
833 
834   for (unsigned I = 0, N = OutputSections.size(); I < N; ++I) {
835     OutputSections[I]->SectionIndex = I + 1;
836     HasRelro |= (Config->ZRelro && isRelroSection(OutputSections[I]));
837   }
838 
839   for (OutputSectionBase<ELFT> *Sec : OutputSections)
840     Out<ELFT>::ShStrTab->reserve(Sec->getName());
841 
842   // Finalizers fix each section's size.
843   // .dynamic section's finalizer may add strings to .dynstr,
844   // so finalize that early.
845   // Likewise, .dynsym is finalized early since that may fill up .gnu.hash.
846   Out<ELFT>::Dynamic->finalize();
847   if (isOutputDynamic())
848     Out<ELFT>::DynSymTab->finalize();
849 
850   // Fill other section headers.
851   for (OutputSectionBase<ELFT> *Sec : OutputSections)
852     Sec->finalize();
853 }
854 
855 // This function add Out<ELFT>::* sections to OutputSections.
856 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
857   auto Add = [&](OutputSectionBase<ELFT> *C) {
858     if (C)
859       OutputSections.push_back(C);
860   };
861 
862   // This order is not the same as the final output order
863   // because we sort the sections using their attributes below.
864   Add(Out<ELFT>::SymTab);
865   Add(Out<ELFT>::ShStrTab);
866   Add(Out<ELFT>::StrTab);
867   if (isOutputDynamic()) {
868     Add(Out<ELFT>::DynSymTab);
869     Add(Out<ELFT>::GnuHashTab);
870     Add(Out<ELFT>::HashTab);
871     Add(Out<ELFT>::Dynamic);
872     Add(Out<ELFT>::DynStrTab);
873     if (Out<ELFT>::RelaDyn->hasRelocs())
874       Add(Out<ELFT>::RelaDyn);
875 
876     // This is a MIPS specific section to hold a space within the data segment
877     // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
878     // See "Dynamic section" in Chapter 5 in the following document:
879     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
880     if (Config->EMachine == EM_MIPS && !Config->Shared) {
881       Out<ELFT>::MipsRldMap = new OutputSection<ELFT>(".rld_map", SHT_PROGBITS,
882                                                       SHF_ALLOC | SHF_WRITE);
883       Out<ELFT>::MipsRldMap->setSize(ELFT::Is64Bits ? 8 : 4);
884       Out<ELFT>::MipsRldMap->updateAlign(ELFT::Is64Bits ? 8 : 4);
885       OwningSections.emplace_back(Out<ELFT>::MipsRldMap);
886       Add(Out<ELFT>::MipsRldMap);
887     }
888   }
889 
890   // We always need to add rel[a].plt to output if it has entries.
891   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
892   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
893     Add(Out<ELFT>::RelaPlt);
894     Out<ELFT>::RelaPlt->Static = !isOutputDynamic();
895   }
896 
897   bool needsGot = !Out<ELFT>::Got->empty();
898   // We add the .got section to the result for dynamic MIPS target because
899   // its address and properties are mentioned in the .dynamic section.
900   if (Config->EMachine == EM_MIPS)
901     needsGot |= isOutputDynamic();
902   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
903   // we need to emit a GOT even if it's empty.
904   if (HasGotOffRel)
905     needsGot = true;
906 
907   if (needsGot)
908     Add(Out<ELFT>::Got);
909   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
910     Add(Out<ELFT>::GotPlt);
911   if (!Out<ELFT>::Plt->empty())
912     Add(Out<ELFT>::Plt);
913 }
914 
915 // The linker is expected to define SECNAME_start and SECNAME_end
916 // symbols for a few sections. This function defines them.
917 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
918   auto Define = [&](StringRef Start, StringRef End,
919                     OutputSectionBase<ELFT> *OS) {
920     if (OS) {
921       Symtab.addSynthetic(Start, *OS, 0);
922       Symtab.addSynthetic(End, *OS, OS->getSize());
923     } else {
924       Symtab.addIgnored(Start);
925       Symtab.addIgnored(End);
926     }
927   };
928 
929   Define("__preinit_array_start", "__preinit_array_end",
930          Out<ELFT>::Dynamic->PreInitArraySec);
931   Define("__init_array_start", "__init_array_end",
932          Out<ELFT>::Dynamic->InitArraySec);
933   Define("__fini_array_start", "__fini_array_end",
934          Out<ELFT>::Dynamic->FiniArraySec);
935 }
936 
937 static bool isAlpha(char C) {
938   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
939 }
940 
941 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
942 
943 // Returns true if S is valid as a C language identifier.
944 static bool isValidCIdentifier(StringRef S) {
945   if (S.empty() || !isAlpha(S[0]))
946     return false;
947   return std::all_of(S.begin() + 1, S.end(), isAlnum);
948 }
949 
950 // If a section name is valid as a C identifier (which is rare because of
951 // the leading '.'), linkers are expected to define __start_<secname> and
952 // __stop_<secname> symbols. They are at beginning and end of the section,
953 // respectively. This is not requested by the ELF standard, but GNU ld and
954 // gold provide the feature, and used by many programs.
955 template <class ELFT>
956 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
957   StringRef S = Sec->getName();
958   if (!isValidCIdentifier(S))
959     return;
960   StringSaver Saver(Alloc);
961   StringRef Start = Saver.save("__start_" + S);
962   StringRef Stop = Saver.save("__stop_" + S);
963   if (SymbolBody *B = Symtab.find(Start))
964     if (B->isUndefined())
965       Symtab.addSynthetic(Start, *Sec, 0);
966   if (SymbolBody *B = Symtab.find(Stop))
967     if (B->isUndefined())
968       Symtab.addSynthetic(Stop, *Sec, Sec->getSize());
969 }
970 
971 template <class ELFT> static bool needsPhdr(OutputSectionBase<ELFT> *Sec) {
972   return Sec->getFlags() & SHF_ALLOC;
973 }
974 
975 static uint32_t toPhdrFlags(uint64_t Flags) {
976   uint32_t Ret = PF_R;
977   if (Flags & SHF_WRITE)
978     Ret |= PF_W;
979   if (Flags & SHF_EXECINSTR)
980     Ret |= PF_X;
981   return Ret;
982 }
983 
984 /// For AMDGPU we need to use custom segment kinds in order to specify which
985 /// address space data should be loaded into.
986 template <class ELFT>
987 static uint32_t getAmdgpuPhdr(OutputSectionBase<ELFT> *Sec) {
988   uint32_t Flags = Sec->getFlags();
989   if (Flags & SHF_AMDGPU_HSA_CODE)
990     return PT_AMDGPU_HSA_LOAD_CODE_AGENT;
991   if ((Flags & SHF_AMDGPU_HSA_GLOBAL) && !(Flags & SHF_AMDGPU_HSA_AGENT))
992     return PT_AMDGPU_HSA_LOAD_GLOBAL_PROGRAM;
993   return PT_LOAD;
994 }
995 
996 template <class ELFT>
997 void Writer<ELFT>::updateRelro(Elf_Phdr *Cur, Elf_Phdr *GnuRelroPhdr,
998                                uintX_t VA) {
999   if (!GnuRelroPhdr->p_type)
1000     setPhdr(GnuRelroPhdr, PT_GNU_RELRO, PF_R, Cur->p_offset, Cur->p_vaddr,
1001             VA - Cur->p_vaddr, 1 /*p_align*/);
1002   GnuRelroPhdr->p_filesz = VA - Cur->p_vaddr;
1003   GnuRelroPhdr->p_memsz = VA - Cur->p_vaddr;
1004 }
1005 
1006 // Visits all sections to create PHDRs and to assign incremental,
1007 // non-overlapping addresses to output sections.
1008 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1009   uintX_t VA = Target->getVAStart() + sizeof(Elf_Ehdr);
1010   uintX_t FileOff = sizeof(Elf_Ehdr);
1011 
1012   // Calculate and reserve the space for the program header first so that
1013   // the first section can start right after the program header.
1014   Phdrs.resize(getPhdrsNum());
1015   size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size();
1016 
1017   // The first phdr entry is PT_PHDR which describes the program header itself.
1018   setPhdr(&Phdrs[0], PT_PHDR, PF_R, FileOff, VA, PhdrSize, /*Align=*/8);
1019   FileOff += PhdrSize;
1020   VA += PhdrSize;
1021 
1022   // PT_INTERP must be the second entry if exists.
1023   int PhdrIdx = 0;
1024   Elf_Phdr *Interp = nullptr;
1025   if (needsInterpSection())
1026     Interp = &Phdrs[++PhdrIdx];
1027 
1028   // Add the first PT_LOAD segment for regular output sections.
1029   setPhdr(&Phdrs[++PhdrIdx], PT_LOAD, PF_R, 0, Target->getVAStart(), FileOff,
1030           Target->getPageSize());
1031 
1032   Elf_Phdr GnuRelroPhdr = {};
1033   Elf_Phdr TlsPhdr{};
1034   bool RelroAligned = false;
1035   uintX_t ThreadBssOffset = 0;
1036   // Create phdrs as we assign VAs and file offsets to all output sections.
1037   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1038     Elf_Phdr *PH = &Phdrs[PhdrIdx];
1039     if (needsPhdr<ELFT>(Sec)) {
1040       uintX_t Flags = toPhdrFlags(Sec->getFlags());
1041       bool InRelRo = Config->ZRelro && (Flags & PF_W) && isRelroSection(Sec);
1042       bool FirstNonRelRo = GnuRelroPhdr.p_type && !InRelRo && !RelroAligned;
1043       if (FirstNonRelRo || PH->p_flags != Flags) {
1044         VA = align(VA, Target->getPageSize());
1045         FileOff = align(FileOff, Target->getPageSize());
1046         if (FirstNonRelRo)
1047           RelroAligned = true;
1048       }
1049 
1050       if (PH->p_flags != Flags) {
1051         // Flags changed. Create a new PT_LOAD.
1052         PH = &Phdrs[++PhdrIdx];
1053         uint32_t PTType = (Config->EMachine != EM_AMDGPU) ? (uint32_t)PT_LOAD
1054                                                           : getAmdgpuPhdr(Sec);
1055         setPhdr(PH, PTType, Flags, FileOff, VA, 0, Target->getPageSize());
1056       }
1057 
1058       if (Sec->getFlags() & SHF_TLS) {
1059         if (!TlsPhdr.p_vaddr)
1060           setPhdr(&TlsPhdr, PT_TLS, PF_R, FileOff, VA, 0, Sec->getAlign());
1061         if (Sec->getType() != SHT_NOBITS)
1062           VA = align(VA, Sec->getAlign());
1063         uintX_t TVA = align(VA + ThreadBssOffset, Sec->getAlign());
1064         Sec->setVA(TVA);
1065         TlsPhdr.p_memsz += Sec->getSize();
1066         if (Sec->getType() == SHT_NOBITS) {
1067           ThreadBssOffset = TVA - VA + Sec->getSize();
1068         } else {
1069           TlsPhdr.p_filesz += Sec->getSize();
1070           VA += Sec->getSize();
1071         }
1072         TlsPhdr.p_align = std::max<uintX_t>(TlsPhdr.p_align, Sec->getAlign());
1073       } else {
1074         VA = align(VA, Sec->getAlign());
1075         Sec->setVA(VA);
1076         VA += Sec->getSize();
1077         if (InRelRo)
1078           updateRelro(PH, &GnuRelroPhdr, VA);
1079       }
1080     }
1081 
1082     FileOff = align(FileOff, Sec->getAlign());
1083     Sec->setFileOffset(FileOff);
1084     if (Sec->getType() != SHT_NOBITS)
1085       FileOff += Sec->getSize();
1086     if (needsPhdr<ELFT>(Sec)) {
1087       PH->p_filesz = FileOff - PH->p_offset;
1088       PH->p_memsz = VA - PH->p_vaddr;
1089     }
1090   }
1091 
1092   if (TlsPhdr.p_vaddr) {
1093     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1094     // so round up the size to make sure the offsets are correct.
1095     TlsPhdr.p_memsz = align(TlsPhdr.p_memsz, TlsPhdr.p_align);
1096     Phdrs[++PhdrIdx] = TlsPhdr;
1097     Out<ELFT>::TlsPhdr = &Phdrs[PhdrIdx];
1098   }
1099 
1100   // Add an entry for .dynamic.
1101   if (isOutputDynamic()) {
1102     Elf_Phdr *PH = &Phdrs[++PhdrIdx];
1103     PH->p_type = PT_DYNAMIC;
1104     copyPhdr(PH, Out<ELFT>::Dynamic);
1105   }
1106 
1107   if (HasRelro) {
1108     Elf_Phdr *PH = &Phdrs[++PhdrIdx];
1109     *PH = GnuRelroPhdr;
1110   }
1111 
1112   // PT_GNU_STACK is a special section to tell the loader to make the
1113   // pages for the stack non-executable.
1114   if (!Config->ZExecStack) {
1115     Elf_Phdr *PH = &Phdrs[++PhdrIdx];
1116     PH->p_type = PT_GNU_STACK;
1117     PH->p_flags = PF_R | PF_W;
1118   }
1119 
1120   // Fix up PT_INTERP as we now know the address of .interp section.
1121   if (Interp) {
1122     Interp->p_type = PT_INTERP;
1123     copyPhdr(Interp, Out<ELFT>::Interp);
1124   }
1125 
1126   // Add space for section headers.
1127   SectionHeaderOff = align(FileOff, ELFT::Is64Bits ? 8 : 4);
1128   FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr);
1129 
1130   // Update "_end" and "end" symbols so that they
1131   // point to the end of the data segment.
1132   ElfSym<ELFT>::End.st_value = VA;
1133 }
1134 
1135 // Returns the number of PHDR entries.
1136 template <class ELFT> int Writer<ELFT>::getPhdrsNum() const {
1137   bool Tls = false;
1138   int I = 2; // 2 for PT_PHDR and first PT_LOAD
1139   if (needsInterpSection())
1140     ++I;
1141   if (isOutputDynamic())
1142     ++I;
1143   if (!Config->ZExecStack)
1144     ++I;
1145   uintX_t Last = PF_R;
1146   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1147     if (!needsPhdr<ELFT>(Sec))
1148       continue;
1149     if (Sec->getFlags() & SHF_TLS)
1150       Tls = true;
1151     uintX_t Flags = toPhdrFlags(Sec->getFlags());
1152     if (Last != Flags) {
1153       Last = Flags;
1154       ++I;
1155     }
1156   }
1157   if (Tls)
1158     ++I;
1159   if (HasRelro)
1160     ++I;
1161   return I;
1162 }
1163 
1164 static uint32_t getELFFlags() {
1165   if (Config->EMachine != EM_MIPS)
1166     return 0;
1167   // FIXME: In fact ELF flags depends on ELF flags of input object files
1168   // and selected emulation. For now just use hard coded values.
1169   uint32_t V = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2;
1170   if (Config->Shared)
1171     V |= EF_MIPS_PIC;
1172   return V;
1173 }
1174 
1175 template <class ELFT>
1176 static typename ELFFile<ELFT>::uintX_t getEntryAddr() {
1177   if (Config->EntrySym) {
1178     if (SymbolBody *E = Config->EntrySym->repl())
1179       return getSymVA<ELFT>(*E);
1180     return 0;
1181   }
1182   if (Config->EntryAddr != uint64_t(-1))
1183     return Config->EntryAddr;
1184   return 0;
1185 }
1186 
1187 // This function is called after we have assigned address and size
1188 // to each section. This function fixes some predefined absolute
1189 // symbol values that depend on section address and size.
1190 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1191   // Update __rel[a]_iplt_{start,end} symbols so that they point
1192   // to beginning or ending of .rela.plt section, respectively.
1193   if (Out<ELFT>::RelaPlt) {
1194     uintX_t Start = Out<ELFT>::RelaPlt->getVA();
1195     ElfSym<ELFT>::RelaIpltStart.st_value = Start;
1196     ElfSym<ELFT>::RelaIpltEnd.st_value = Start + Out<ELFT>::RelaPlt->getSize();
1197   }
1198 
1199   // Update MIPS _gp absolute symbol so that it points to the static data.
1200   if (Config->EMachine == EM_MIPS)
1201     ElfSym<ELFT>::MipsGp.st_value = getMipsGpAddr<ELFT>();
1202 }
1203 
1204 template <class ELFT> void Writer<ELFT>::writeHeader() {
1205   uint8_t *Buf = Buffer->getBufferStart();
1206   memcpy(Buf, "\177ELF", 4);
1207 
1208   // Write the ELF header.
1209   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1210   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1211   EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little
1212                                ? ELFDATA2LSB
1213                                : ELFDATA2MSB;
1214   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1215 
1216   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1217   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1218 
1219   EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC;
1220   EHdr->e_machine = FirstObj.getEMachine();
1221   EHdr->e_version = EV_CURRENT;
1222   EHdr->e_entry = getEntryAddr<ELFT>();
1223   EHdr->e_phoff = sizeof(Elf_Ehdr);
1224   EHdr->e_shoff = SectionHeaderOff;
1225   EHdr->e_flags = getELFFlags();
1226   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1227   EHdr->e_phentsize = sizeof(Elf_Phdr);
1228   EHdr->e_phnum = Phdrs.size();
1229   EHdr->e_shentsize = sizeof(Elf_Shdr);
1230   EHdr->e_shnum = getNumSections();
1231   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1232 
1233   // Write the program header table.
1234   memcpy(Buf + EHdr->e_phoff, &Phdrs[0], Phdrs.size() * sizeof(Phdrs[0]));
1235 
1236   // Write the section header table. Note that the first table entry is null.
1237   auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1238   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1239     Sec->writeHeaderTo(++SHdrs);
1240 }
1241 
1242 template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
1243   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1244       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
1245   error(BufferOrErr, "failed to open " + Path);
1246   Buffer = std::move(*BufferOrErr);
1247 }
1248 
1249 // Write section contents to a mmap'ed file.
1250 template <class ELFT> void Writer<ELFT>::writeSections() {
1251   uint8_t *Buf = Buffer->getBufferStart();
1252 
1253   // PPC64 needs to process relocations in the .opd section before processing
1254   // relocations in code-containing sections.
1255   if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) {
1256     Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
1257     Sec->writeTo(Buf + Sec->getFileOff());
1258   }
1259 
1260   // Write all sections but string table sections. We know the sizes of the
1261   // string tables already, but they may not have actual strings yet (only
1262   // room may be reserved), because writeTo() is allowed to add actual
1263   // strings to the string tables.
1264   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1265     if (Sec != Out<ELFT>::Opd && Sec->getType() != SHT_STRTAB)
1266       Sec->writeTo(Buf + Sec->getFileOff());
1267 
1268   // Write string table sections.
1269   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1270     if (Sec != Out<ELFT>::Opd && Sec->getType() == SHT_STRTAB)
1271       Sec->writeTo(Buf + Sec->getFileOff());
1272 }
1273 
1274 template <class ELFT>
1275 void Writer<ELFT>::setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags,
1276                            uintX_t FileOff, uintX_t VA, uintX_t Size,
1277                            uintX_t Align) {
1278   PH->p_type = Type;
1279   PH->p_flags = Flags;
1280   PH->p_offset = FileOff;
1281   PH->p_vaddr = VA;
1282   PH->p_paddr = VA;
1283   PH->p_filesz = Size;
1284   PH->p_memsz = Size;
1285   PH->p_align = Align;
1286 }
1287 
1288 template <class ELFT>
1289 void Writer<ELFT>::copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT> *From) {
1290   PH->p_flags = toPhdrFlags(From->getFlags());
1291   PH->p_offset = From->getFileOff();
1292   PH->p_vaddr = From->getVA();
1293   PH->p_paddr = From->getVA();
1294   PH->p_filesz = From->getSize();
1295   PH->p_memsz = From->getSize();
1296   PH->p_align = From->getAlign();
1297 }
1298 
1299 template <class ELFT> void Writer<ELFT>::buildSectionMap() {
1300   for (const std::pair<StringRef, std::vector<StringRef>> &OutSec :
1301        Config->OutputSections)
1302     for (StringRef Name : OutSec.second)
1303       InputToOutputSection[Name] = OutSec.first;
1304 }
1305 
1306 template void elf2::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
1307 template void elf2::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
1308 template void elf2::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
1309 template void elf2::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
1310