xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 928c8254)
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/Endian.h"
21 #include "llvm/Support/FileOutputBuffer.h"
22 #include "llvm/Support/StringSaver.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 using namespace llvm;
26 using namespace llvm::ELF;
27 using namespace llvm::object;
28 using namespace llvm::support::endian;
29 
30 using namespace lld;
31 using namespace lld::elf;
32 
33 namespace {
34 // The writer writes a SymbolTable result to a file.
35 template <class ELFT> class Writer {
36 public:
37   typedef typename ELFT::uint uintX_t;
38   typedef typename ELFT::Shdr Elf_Shdr;
39   typedef typename ELFT::Ehdr Elf_Ehdr;
40   typedef typename ELFT::Phdr Elf_Phdr;
41   typedef typename ELFT::Sym Elf_Sym;
42   typedef typename ELFT::SymRange Elf_Sym_Range;
43   typedef typename ELFT::Rela Elf_Rela;
44   Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
45   void run();
46 
47 private:
48   // This describes a program header entry.
49   // Each contains type, access flags and range of output sections that will be
50   // placed in it.
51   struct Phdr {
52     Phdr(unsigned Type, unsigned Flags) {
53       H.p_type = Type;
54       H.p_flags = Flags;
55     }
56     Elf_Phdr H = {};
57     OutputSectionBase<ELFT> *First = nullptr;
58     OutputSectionBase<ELFT> *Last = nullptr;
59   };
60 
61   void copyLocalSymbols();
62   void addReservedSymbols();
63   void createSections();
64   void addPredefinedSections();
65   bool needsGot();
66 
67   template <class RelTy>
68   void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels);
69 
70   void scanRelocs(InputSection<ELFT> &C);
71   void scanRelocs(InputSectionBase<ELFT> &S, const Elf_Shdr &RelSec);
72   void createPhdrs();
73   void assignAddresses();
74   void assignFileOffsets();
75   void setPhdrs();
76   void fixHeaders();
77   void fixSectionAlignments();
78   void fixAbsoluteSymbols();
79   void openFile();
80   void writeHeader();
81   void writeSections();
82   void writeBuildId();
83   bool isDiscarded(InputSectionBase<ELFT> *IS) const;
84   StringRef getOutputSectionName(InputSectionBase<ELFT> *S) const;
85   bool needsInterpSection() const {
86     return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
87   }
88   bool isOutputDynamic() const {
89     return !Symtab.getSharedFiles().empty() || Config->Pic;
90   }
91   template <class RelTy>
92   void scanRelocsForThunks(const elf::ObjectFile<ELFT> &File,
93                            ArrayRef<RelTy> Rels);
94 
95   void ensureBss();
96   void addCommonSymbols(std::vector<DefinedCommon *> &Syms);
97   void addCopyRelSymbol(SharedSymbol<ELFT> *Sym);
98 
99   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
100 
101   BumpPtrAllocator Alloc;
102   std::vector<OutputSectionBase<ELFT> *> OutputSections;
103   std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
104 
105   void addRelIpltSymbols();
106   void addStartEndSymbols();
107   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
108 
109   SymbolTable<ELFT> &Symtab;
110   std::vector<Phdr> Phdrs;
111 
112   uintX_t FileSize;
113   uintX_t SectionHeaderOff;
114 
115   // Flag to force GOT to be in output if we have relocations
116   // that relies on its address.
117   bool HasGotOffRel = false;
118 };
119 } // anonymous namespace
120 
121 template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) {
122   typedef typename ELFT::uint uintX_t;
123   typedef typename ELFT::Ehdr Elf_Ehdr;
124 
125   // Create singleton output sections.
126   DynamicSection<ELFT> Dynamic(*Symtab);
127   EhFrameHeader<ELFT> EhFrameHdr;
128   GotSection<ELFT> Got;
129   InterpSection<ELFT> Interp;
130   PltSection<ELFT> Plt;
131   RelocationSection<ELFT> RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn");
132   StringTableSection<ELFT> DynStrTab(".dynstr", true);
133   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
134   SymbolTableSection<ELFT> DynSymTab(*Symtab, DynStrTab);
135   VersionTableSection<ELFT> VerSym;
136   VersionNeedSection<ELFT> VerNeed;
137 
138   OutputSectionBase<ELFT> ElfHeader("", 0, SHF_ALLOC);
139   ElfHeader.setSize(sizeof(Elf_Ehdr));
140   OutputSectionBase<ELFT> ProgramHeaders("", 0, SHF_ALLOC);
141   ProgramHeaders.updateAlign(sizeof(uintX_t));
142 
143   // Instantiate optional output sections if they are needed.
144   std::unique_ptr<BuildIdSection<ELFT>> BuildId;
145   std::unique_ptr<GnuHashTableSection<ELFT>> GnuHashTab;
146   std::unique_ptr<GotPltSection<ELFT>> GotPlt;
147   std::unique_ptr<HashTableSection<ELFT>> HashTab;
148   std::unique_ptr<RelocationSection<ELFT>> RelaPlt;
149   std::unique_ptr<StringTableSection<ELFT>> StrTab;
150   std::unique_ptr<SymbolTableSection<ELFT>> SymTabSec;
151   std::unique_ptr<OutputSection<ELFT>> MipsRldMap;
152 
153   if (Config->BuildId == BuildIdKind::Fnv1)
154     BuildId.reset(new BuildIdFnv1<ELFT>);
155   else if (Config->BuildId == BuildIdKind::Md5)
156     BuildId.reset(new BuildIdMd5<ELFT>);
157   else if (Config->BuildId == BuildIdKind::Sha1)
158     BuildId.reset(new BuildIdSha1<ELFT>);
159 
160   if (Config->GnuHash)
161     GnuHashTab.reset(new GnuHashTableSection<ELFT>);
162   if (Config->SysvHash)
163     HashTab.reset(new HashTableSection<ELFT>);
164   if (Target->UseLazyBinding) {
165     StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
166     GotPlt.reset(new GotPltSection<ELFT>);
167     RelaPlt.reset(new RelocationSection<ELFT>(S));
168   }
169   if (!Config->StripAll) {
170     StrTab.reset(new StringTableSection<ELFT>(".strtab", false));
171     SymTabSec.reset(new SymbolTableSection<ELFT>(*Symtab, *StrTab));
172   }
173   if (Config->EMachine == EM_MIPS && !Config->Shared) {
174     // This is a MIPS specific section to hold a space within the data segment
175     // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
176     // See "Dynamic section" in Chapter 5 in the following document:
177     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
178     MipsRldMap.reset(new OutputSection<ELFT>(".rld_map", SHT_PROGBITS,
179                                              SHF_ALLOC | SHF_WRITE));
180     MipsRldMap->setSize(sizeof(uintX_t));
181     MipsRldMap->updateAlign(sizeof(uintX_t));
182   }
183 
184   Out<ELFT>::BuildId = BuildId.get();
185   Out<ELFT>::DynStrTab = &DynStrTab;
186   Out<ELFT>::DynSymTab = &DynSymTab;
187   Out<ELFT>::Dynamic = &Dynamic;
188   Out<ELFT>::EhFrameHdr = &EhFrameHdr;
189   Out<ELFT>::GnuHashTab = GnuHashTab.get();
190   Out<ELFT>::Got = &Got;
191   Out<ELFT>::GotPlt = GotPlt.get();
192   Out<ELFT>::HashTab = HashTab.get();
193   Out<ELFT>::Interp = &Interp;
194   Out<ELFT>::Plt = &Plt;
195   Out<ELFT>::RelaDyn = &RelaDyn;
196   Out<ELFT>::RelaPlt = RelaPlt.get();
197   Out<ELFT>::ShStrTab = &ShStrTab;
198   Out<ELFT>::StrTab = StrTab.get();
199   Out<ELFT>::SymTab = SymTabSec.get();
200   Out<ELFT>::VerSym = &VerSym;
201   Out<ELFT>::VerNeed = &VerNeed;
202   Out<ELFT>::Bss = nullptr;
203   Out<ELFT>::MipsRldMap = MipsRldMap.get();
204   Out<ELFT>::Opd = nullptr;
205   Out<ELFT>::OpdBuf = nullptr;
206   Out<ELFT>::TlsPhdr = nullptr;
207   Out<ELFT>::ElfHeader = &ElfHeader;
208   Out<ELFT>::ProgramHeaders = &ProgramHeaders;
209 
210   Writer<ELFT>(*Symtab).run();
211 }
212 
213 // The main function of the writer.
214 template <class ELFT> void Writer<ELFT>::run() {
215   if (!Config->DiscardAll)
216     copyLocalSymbols();
217   addReservedSymbols();
218   createSections();
219   if (HasError)
220     return;
221 
222   if (Config->Relocatable) {
223     assignFileOffsets();
224   } else {
225     createPhdrs();
226     fixHeaders();
227     if (ScriptConfig->DoLayout) {
228       Script<ELFT>::X->assignAddresses(OutputSections);
229     } else {
230       fixSectionAlignments();
231       assignAddresses();
232     }
233     assignFileOffsets();
234     setPhdrs();
235     fixAbsoluteSymbols();
236   }
237 
238   openFile();
239   if (HasError)
240     return;
241   writeHeader();
242   writeSections();
243   writeBuildId();
244   if (HasError)
245     return;
246   check(Buffer->commit());
247 }
248 
249 namespace {
250 template <bool Is64Bits> struct SectionKey {
251   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
252   StringRef Name;
253   uint32_t Type;
254   uintX_t Flags;
255   uintX_t Alignment;
256 };
257 }
258 namespace llvm {
259 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
260   static SectionKey<Is64Bits> getEmptyKey() {
261     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0,
262                                 0};
263   }
264   static SectionKey<Is64Bits> getTombstoneKey() {
265     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
266                                 0, 0};
267   }
268   static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
269     return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
270   }
271   static bool isEqual(const SectionKey<Is64Bits> &LHS,
272                       const SectionKey<Is64Bits> &RHS) {
273     return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
274            LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
275            LHS.Alignment == RHS.Alignment;
276   }
277 };
278 }
279 
280 // Returns the number of relocations processed.
281 template <class ELFT>
282 static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
283                                     InputSectionBase<ELFT> &C,
284                                     typename ELFT::uint Offset,
285                                     typename ELFT::uint Addend, RelExpr Expr) {
286   if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
287     return 0;
288 
289   if (!Body.isTls())
290     return 0;
291 
292   typedef typename ELFT::uint uintX_t;
293   if (Expr == R_TLSLD_PC || Expr == R_TLSLD) {
294     // Local-Dynamic relocs can be relaxed to Local-Exec.
295     if (!Config->Shared) {
296       C.Relocations.push_back(
297           {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
298       return 2;
299     }
300     if (Out<ELFT>::Got->addTlsIndex())
301       Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
302                                     Out<ELFT>::Got->getTlsIndexOff(), false,
303                                     nullptr, 0});
304     C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
305     return 1;
306   }
307 
308   // Local-Dynamic relocs can be relaxed to Local-Exec.
309   if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) {
310     C.Relocations.push_back(
311         {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
312     return 1;
313   }
314 
315   if (Target->isTlsGlobalDynamicRel(Type)) {
316     if (Config->Shared) {
317       if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
318         uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
319         Out<ELFT>::RelaDyn->addReloc(
320             {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
321         Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
322                                       Off + (uintX_t)sizeof(uintX_t), false,
323                                       &Body, 0});
324       }
325       C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
326       return 1;
327     }
328 
329     // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
330     // depending on the symbol being locally defined or not.
331     if (Body.isPreemptible()) {
332       Expr =
333           Expr == R_TLSGD_PC ? R_RELAX_TLS_GD_TO_IE_PC : R_RELAX_TLS_GD_TO_IE;
334       C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
335       if (!Body.isInGot()) {
336         Out<ELFT>::Got->addEntry(Body);
337         Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got,
338                                       Body.getGotOffset<ELFT>(), false, &Body,
339                                       0});
340       }
341       return 2;
342     }
343     C.Relocations.push_back(
344         {R_RELAX_TLS_GD_TO_LE, Type, Offset, Addend, &Body});
345     return Target->TlsGdToLeSkip;
346   }
347 
348   // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
349   // defined.
350   if (Target->isTlsInitialExecRel(Type) && !Config->Shared &&
351       !Body.isPreemptible()) {
352     C.Relocations.push_back(
353         {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
354     return 1;
355   }
356   return 0;
357 }
358 
359 // Some targets might require creation of thunks for relocations. Now we
360 // support only MIPS which requires LA25 thunk to call PIC code from non-PIC
361 // one. Scan relocations to find each one requires thunk.
362 template <class ELFT>
363 template <class RelTy>
364 void Writer<ELFT>::scanRelocsForThunks(const elf::ObjectFile<ELFT> &File,
365                                        ArrayRef<RelTy> Rels) {
366   for (const RelTy &RI : Rels) {
367     uint32_t Type = RI.getType(Config->Mips64EL);
368     SymbolBody &Body = File.getRelocTargetSym(RI);
369     if (Body.hasThunk() || !Target->needsThunk(Type, File, Body))
370       continue;
371     auto *D = cast<DefinedRegular<ELFT>>(&Body);
372     auto *S = cast<InputSection<ELFT>>(D->Section);
373     S->addThunk(Body);
374   }
375 }
376 
377 template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
378   return read32<E>(Loc) & 0xffff;
379 }
380 
381 template <class RelTy>
382 static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
383   switch (Rel->getType(Config->Mips64EL)) {
384   case R_MIPS_HI16:
385     return R_MIPS_LO16;
386   case R_MIPS_GOT16:
387     return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
388   case R_MIPS_PCHI16:
389     return R_MIPS_PCLO16;
390   case R_MICROMIPS_HI16:
391     return R_MICROMIPS_LO16;
392   default:
393     return R_MIPS_NONE;
394   }
395 }
396 
397 template <class ELFT, class RelTy>
398 static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
399                                     SymbolBody &Sym, const RelTy *Rel,
400                                     const RelTy *End) {
401   uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
402   uint32_t Type = getMipsPairType(Rel, Sym);
403 
404   // Some MIPS relocations use addend calculated from addend of the relocation
405   // itself and addend of paired relocation. ABI requires to compute such
406   // combined addend in case of REL relocation record format only.
407   // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
408   if (RelTy::IsRela || Type == R_MIPS_NONE)
409     return 0;
410 
411   for (const RelTy *RI = Rel; RI != End; ++RI) {
412     if (RI->getType(Config->Mips64EL) != Type)
413       continue;
414     if (RI->getSymbol(Config->Mips64EL) != SymIndex)
415       continue;
416     const endianness E = ELFT::TargetEndianness;
417     return ((read32<E>(BufLoc) & 0xffff) << 16) +
418            readSignedLo16<E>(Buf + RI->r_offset);
419   }
420   unsigned OldType = Rel->getType(Config->Mips64EL);
421   StringRef OldName = getELFRelocationTypeName(Config->EMachine, OldType);
422   StringRef NewName = getELFRelocationTypeName(Config->EMachine, Type);
423   warning("can't find matching " + NewName + " relocation for " + OldName);
424   return 0;
425 }
426 
427 // True if non-preemptable symbol always has the same value regardless of where
428 // the DSO is loaded.
429 template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
430   Symbol *Sym = Body.Backref;
431   if (Body.isUndefined()) {
432     if (!Sym)
433       return false; // undefined local. That is the dummy symbol 0.
434     if (Sym->isWeak())
435       return true; // always 0
436   }
437   if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body))
438     return DR->Section == nullptr; // Absolute symbol.
439   return false;
440 }
441 
442 namespace {
443 enum PltNeed { Plt_No, Plt_Explicit, Plt_Implicit };
444 }
445 
446 static bool needsPlt(RelExpr Expr) {
447   return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT;
448 }
449 
450 static PltNeed needsPlt(RelExpr Expr, uint32_t Type, const SymbolBody &S) {
451   if (S.isGnuIFunc())
452     return Plt_Explicit;
453   if (S.isPreemptible() && needsPlt(Expr))
454     return Plt_Explicit;
455 
456   // This handles a non PIC program call to function in a shared library.
457   // In an ideal world, we could just report an error saying the relocation
458   // can overflow at runtime.
459   // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
460   // libc.so.
461   //
462   // The general idea on how to handle such cases is to create a PLT entry
463   // and use that as the function value.
464   //
465   // For the static linking part, we just return true and everything else
466   // will use the the PLT entry as the address.
467   //
468   // The remaining problem is making sure pointer equality still works. We
469   // need the help of the dynamic linker for that. We let it know that we have
470   // a direct reference to a so symbol by creating an undefined symbol with a
471   // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
472   // the value of the symbol we created. This is true even for got entries, so
473   // pointer equality is maintained. To avoid an infinite loop, the only entry
474   // that points to the real function is a dedicated got entry used by the
475   // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
476   // R_386_JMP_SLOT, etc).
477   if (S.isShared() && !Config->Pic && S.isFunc())
478     if (!refersToGotEntry(Expr))
479       return Plt_Implicit;
480 
481   return Plt_No;
482 }
483 
484 static bool needsCopyRel(RelExpr E, const SymbolBody &S) {
485   if (Config->Shared)
486     return false;
487   if (!S.isShared())
488     return false;
489   if (!S.isObject())
490     return false;
491   if (refersToGotEntry(E))
492     return false;
493   if (needsPlt(E))
494     return false;
495   if (E == R_SIZE)
496     return false;
497   return true;
498 }
499 
500 template <class ELFT>
501 static bool isRelRelative(RelExpr E, uint32_t Type, const SymbolBody &Body) {
502   if (E == R_SIZE)
503     return true;
504 
505   bool AbsVal = (isAbsolute<ELFT>(Body) || Body.isTls()) &&
506                 !refersToGotEntry(E) && !needsPlt(E);
507 
508   bool RelE = E == R_PC || E == R_PLT_PC || E == R_GOT_PC || E == R_GOTREL ||
509               E == R_PAGE_PC;
510   if (AbsVal && !RelE)
511     return true;
512   if (!AbsVal && RelE)
513     return true;
514 
515   return Target->isRelRelative(Type);
516 }
517 
518 // The reason we have to do this early scan is as follows
519 // * To mmap the output file, we need to know the size
520 // * For that, we need to know how many dynamic relocs we will have.
521 // It might be possible to avoid this by outputting the file with write:
522 // * Write the allocated output sections, computing addresses.
523 // * Apply relocations, recording which ones require a dynamic reloc.
524 // * Write the dynamic relocations.
525 // * Write the rest of the file.
526 // This would have some drawbacks. For example, we would only know if .rela.dyn
527 // is needed after applying relocations. If it is, it will go after rw and rx
528 // sections. Given that it is ro, we will need an extra PT_LOAD. This
529 // complicates things for the dynamic linker and means we would have to reserve
530 // space for the extra PT_LOAD even if we end up not using it.
531 template <class ELFT>
532 template <class RelTy>
533 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
534   uintX_t Flags = C.getSectionHdr()->sh_flags;
535   bool IsAlloc = Flags & SHF_ALLOC;
536   bool IsWrite = Flags & SHF_WRITE;
537 
538   auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
539     if (IsAlloc)
540       Out<ELFT>::RelaDyn->addReloc(Reloc);
541   };
542 
543   const elf::ObjectFile<ELFT> &File = *C.getFile();
544   ArrayRef<uint8_t> SectionData = C.getSectionData();
545   const uint8_t *Buf = SectionData.begin();
546   for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
547     const RelTy &RI = *I;
548     SymbolBody &Body = File.getRelocTargetSym(RI);
549     uint32_t Type = RI.getType(Config->Mips64EL);
550 
551     // Ignore "hint" relocation because it is for optional code optimization.
552     if (Target->isHintRel(Type))
553       continue;
554 
555     uintX_t Offset = C.getOffset(RI.r_offset);
556     if (Offset == (uintX_t)-1)
557       continue;
558 
559     RelExpr Expr = Target->getRelExpr(Type, Body);
560 
561     // This relocation does not require got entry, but it is relative to got and
562     // needs it to be created. Here we request for that.
563     if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC)
564       HasGotOffRel = true;
565 
566     uintX_t Addend = getAddend<ELFT>(RI);
567     const uint8_t *BufLoc = Buf + RI.r_offset;
568     if (!RelTy::IsRela)
569       Addend += Target->getImplicitAddend(BufLoc, Type);
570     if (Config->EMachine == EM_MIPS) {
571       Addend += findMipsPairedAddend<ELFT>(Buf, BufLoc, Body, &RI, E);
572       if (Type == R_MIPS_LO16 && Expr == R_PC)
573         // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
574         // symbol. In that case we should use the following formula for
575         // calculation "AHL + GP – P + 4". Let's add 4 right here.
576         // For details see p. 4-19 at
577         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
578         Addend += 4;
579     }
580 
581     if (unsigned Processed =
582             handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
583       I += (Processed - 1);
584       continue;
585     }
586 
587     if (Expr == R_GOT && !isRelRelative<ELFT>(Expr, Type, Body) &&
588         Config->Shared)
589       AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body,
590               getAddend<ELFT>(RI)});
591 
592     // If a symbol in a DSO is referenced directly instead of through GOT
593     // in a read-only section, we need to create a copy relocation for the
594     // symbol.
595     if (auto *B = dyn_cast<SharedSymbol<ELFT>>(&Body)) {
596       if (IsAlloc && !IsWrite && needsCopyRel(Expr, *B)) {
597         if (!B->needsCopy())
598           addCopyRelSymbol(B);
599         C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
600         continue;
601       }
602     }
603 
604     bool Preemptible = Body.isPreemptible();
605 
606     // If a relocation needs PLT, we create a PLT and a GOT slot
607     // for the symbol.
608     PltNeed NeedPlt = needsPlt(Expr, Type, Body);
609     if (NeedPlt) {
610       if (NeedPlt == Plt_Implicit)
611         Body.NeedsCopyOrPltAddr = true;
612       RelExpr E = Expr;
613       if (Expr == R_PPC_OPD)
614         E = R_PPC_PLT_OPD;
615       else if (Expr == R_PC)
616         E = R_PLT_PC;
617       else if (Expr == R_ABS)
618         E = R_PLT;
619       C.Relocations.push_back({E, Type, Offset, Addend, &Body});
620 
621       if (Body.isInPlt())
622         continue;
623       Out<ELFT>::Plt->addEntry(Body);
624 
625       uint32_t Rel;
626       if (Body.isGnuIFunc())
627         Rel = Preemptible ? Target->PltRel : Target->IRelativeRel;
628       else
629         Rel = Target->UseLazyBinding ? Target->PltRel : Target->GotRel;
630 
631       if (Target->UseLazyBinding) {
632         Out<ELFT>::GotPlt->addEntry(Body);
633         if (IsAlloc)
634           Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
635                                         Body.getGotPltOffset<ELFT>(),
636                                         !Preemptible, &Body, 0});
637       } else {
638         if (Body.isInGot())
639           continue;
640         Out<ELFT>::Got->addEntry(Body);
641         AddDyn({Rel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), !Preemptible,
642                 &Body, 0});
643       }
644       continue;
645     }
646 
647     // We decided not to use a plt. Optimize a reference to the plt to a
648     // reference to the symbol itself.
649     if (Expr == R_PLT_PC)
650       Expr = R_PC;
651     if (Expr == R_PPC_PLT_OPD)
652       Expr = R_PPC_OPD;
653     if (Expr == R_PLT)
654       Expr = R_ABS;
655 
656     if (Target->needsThunk(Type, File, Body)) {
657       C.Relocations.push_back({R_THUNK, Type, Offset, Addend, &Body});
658       continue;
659     }
660 
661     // If a relocation needs GOT, we create a GOT slot for the symbol.
662     if (refersToGotEntry(Expr)) {
663       uint32_t T = Body.isTls() ? Target->getTlsGotRel(Type) : Type;
664       if (Config->EMachine == EM_MIPS && Expr == R_GOT_OFF)
665         Addend -= MipsGPOffset;
666       C.Relocations.push_back({Expr, T, Offset, Addend, &Body});
667       if (Body.isInGot())
668         continue;
669       Out<ELFT>::Got->addEntry(Body);
670 
671       if (Config->EMachine == EM_MIPS)
672         // MIPS ABI has special rules to process GOT entries
673         // and doesn't require relocation entries for them.
674         // See "Global Offset Table" in Chapter 5 in the following document
675         // for detailed description:
676         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
677         continue;
678 
679       if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) {
680         uint32_t DynType;
681         if (Body.isTls())
682           DynType = Target->TlsGotRel;
683         else if (Preemptible)
684           DynType = Target->GotRel;
685         else
686           DynType = Target->RelativeRel;
687         AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
688                 !Preemptible, &Body, 0});
689       }
690       continue;
691     }
692 
693     if (Preemptible) {
694       // We don't know anything about the finaly symbol. Just ask the dynamic
695       // linker to handle the relocation for us.
696       AddDyn({Target->getDynRel(Type), C.OutSec, Offset, false, &Body, Addend});
697       // MIPS ABI turns using of GOT and dynamic relocations inside out.
698       // While regular ABI uses dynamic relocations to fill up GOT entries
699       // MIPS ABI requires dynamic linker to fills up GOT entries using
700       // specially sorted dynamic symbol table. This affects even dynamic
701       // relocations against symbols which do not require GOT entries
702       // creation explicitly, i.e. do not have any GOT-relocations. So if
703       // a preemptible symbol has a dynamic relocation we anyway have
704       // to create a GOT entry for it.
705       // If a non-preemptible symbol has a dynamic relocation against it,
706       // dynamic linker takes it st_value, adds offset and writes down
707       // result of the dynamic relocation. In case of preemptible symbol
708       // dynamic linker performs symbol resolution, writes the symbol value
709       // to the GOT entry and reads the GOT entry when it needs to perform
710       // a dynamic relocation.
711       // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
712       if (Config->EMachine == EM_MIPS && !Body.isInGot())
713         Out<ELFT>::Got->addEntry(Body);
714       continue;
715     }
716 
717     // We know that this is the final symbol. If the program being produced
718     // is position independent, the final value is still not known.
719     // If the relocation depends on the symbol value (not the size or distances
720     // in the output), we still need some help from the dynamic linker.
721     // We can however do better than just copying the incoming relocation. We
722     // can process some of it and and just ask the dynamic linker to add the
723     // load address.
724     if (!Config->Pic || isRelRelative<ELFT>(Expr, Type, Body)) {
725       if (Config->EMachine == EM_MIPS && Body.isLocal() &&
726           (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32))
727         Addend += File.getMipsGp0();
728       C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
729       continue;
730     }
731 
732     if (Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
733       Addend += getPPC64TocBase();
734     AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, Addend});
735     C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
736   }
737 
738   // Scan relocations for necessary thunks.
739   if (Config->EMachine == EM_MIPS)
740     scanRelocsForThunks(File, Rels);
741 }
742 
743 template <class ELFT> void Writer<ELFT>::scanRelocs(InputSection<ELFT> &C) {
744   // Scan all relocations. Each relocation goes through a series
745   // of tests to determine if it needs special treatment, such as
746   // creating GOT, PLT, copy relocations, etc.
747   //
748   // The current code is a bit wasteful because it scans relocations
749   // in non-SHF_ALLOC sections. Such sections are never mapped to
750   // memory at runtime. Debug section is an example. Relocations in
751   // non-alloc sections are much easier to handle because it will
752   // never need complex treatement such as GOT or PLT (because at
753   // runtime no one refers them). We probably should skip non-alloc
754   // sections here and directly handle non-alloc relocations in
755   // writeTo function.
756   for (const Elf_Shdr *RelSec : C.RelocSections)
757     scanRelocs(C, *RelSec);
758 }
759 
760 template <class ELFT>
761 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &S,
762                               const Elf_Shdr &RelSec) {
763   ELFFile<ELFT> &EObj = S.getFile()->getObj();
764   if (RelSec.sh_type == SHT_RELA)
765     scanRelocs(S, EObj.relas(&RelSec));
766   else
767     scanRelocs(S, EObj.rels(&RelSec));
768 }
769 
770 template <class ELFT>
771 static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) {
772   if (!Config->NoUndefined) {
773     if (Config->Relocatable)
774       return;
775     if (Config->Shared)
776       if (Sym->Backref->Visibility == STV_DEFAULT)
777         return;
778   }
779 
780   std::string Msg = "undefined symbol: " + Sym->getName().str();
781   if (InputFile *File = Symtab.findFile(Sym))
782     Msg += " in " + File->getName().str();
783   if (Config->NoinhibitExec)
784     warning(Msg);
785   else
786     error(Msg);
787 }
788 
789 template <class ELFT>
790 static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
791                                const SymbolBody &B) {
792   if (B.isFile())
793     return false;
794 
795   // We keep sections in symtab for relocatable output.
796   if (B.isSection())
797     return Config->Relocatable;
798 
799   // If sym references a section in a discarded group, don't keep it.
800   if (Sec == &InputSection<ELFT>::Discarded)
801     return false;
802 
803   if (Config->DiscardNone)
804     return true;
805 
806   // In ELF assembly .L symbols are normally discarded by the assembler.
807   // If the assembler fails to do so, the linker discards them if
808   // * --discard-locals is used.
809   // * The symbol is in a SHF_MERGE section, which is normally the reason for
810   //   the assembler keeping the .L symbol.
811   if (!SymName.startswith(".L") && !SymName.empty())
812     return true;
813 
814   if (Config->DiscardLocals)
815     return false;
816 
817   return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
818 }
819 
820 // Local symbols are not in the linker's symbol table. This function scans
821 // each object file's symbol table to copy local symbols to the output.
822 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
823   if (!Out<ELFT>::SymTab)
824     return;
825   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
826        Symtab.getObjectFiles()) {
827     const char *StrTab = F->getStringTable().data();
828     for (SymbolBody *B : F->getLocalSymbols()) {
829       auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
830       // No reason to keep local undefined symbol in symtab.
831       if (!DR)
832         continue;
833       StringRef SymName(StrTab + B->getNameOffset());
834       InputSectionBase<ELFT> *Sec = DR->Section;
835       if (!shouldKeepInSymtab<ELFT>(Sec, SymName, *B))
836         continue;
837       if (Sec) {
838         if (!Sec->Live)
839           continue;
840 
841         // Garbage collection is normally able to remove local symbols if they
842         // point to gced sections. In the case of SHF_MERGE sections, we want it
843         // to also be able to drop them if part of the section is gced.
844         // We could look at the section offset map to keep some of these
845         // symbols, but almost all local symbols are .L* symbols, so it
846         // is probably not worth the complexity.
847         if (Config->GcSections && isa<MergeInputSection<ELFT>>(Sec))
848           continue;
849       }
850       ++Out<ELFT>::SymTab->NumLocals;
851       if (Config->Relocatable)
852         B->DynsymIndex = Out<ELFT>::SymTab->NumLocals;
853       F->KeptLocalSyms.push_back(
854           std::make_pair(DR, Out<ELFT>::SymTab->StrTabSec.addString(SymName)));
855     }
856   }
857 }
858 
859 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
860 // we would like to make sure appear is a specific order to maximize their
861 // coverage by a single signed 16-bit offset from the TOC base pointer.
862 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
863 // sections. This will put it next to the loaded special PPC64 sections (and,
864 // thus, within reach of the TOC base pointer).
865 static int getPPC64SectionRank(StringRef SectionName) {
866   return StringSwitch<int>(SectionName)
867       .Case(".tocbss", 0)
868       .Case(".branch_lt", 2)
869       .Case(".toc", 3)
870       .Case(".toc1", 4)
871       .Case(".opd", 5)
872       .Default(1);
873 }
874 
875 template <class ELFT> static bool isRelroSection(OutputSectionBase<ELFT> *Sec) {
876   if (!Config->ZRelro)
877     return false;
878   typename ELFT::uint Flags = Sec->getFlags();
879   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
880     return false;
881   if (Flags & SHF_TLS)
882     return true;
883   uint32_t Type = Sec->getType();
884   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
885       Type == SHT_PREINIT_ARRAY)
886     return true;
887   if (Sec == Out<ELFT>::GotPlt)
888     return Config->ZNow;
889   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
890     return true;
891   StringRef S = Sec->getName();
892   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
893          S == ".eh_frame";
894 }
895 
896 // Output section ordering is determined by this function.
897 template <class ELFT>
898 static bool compareSections(OutputSectionBase<ELFT> *A,
899                             OutputSectionBase<ELFT> *B) {
900   typedef typename ELFT::uint uintX_t;
901 
902   int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName());
903   if (Comp != 0)
904     return Comp < 0;
905 
906   uintX_t AFlags = A->getFlags();
907   uintX_t BFlags = B->getFlags();
908 
909   // Allocatable sections go first to reduce the total PT_LOAD size and
910   // so debug info doesn't change addresses in actual code.
911   bool AIsAlloc = AFlags & SHF_ALLOC;
912   bool BIsAlloc = BFlags & SHF_ALLOC;
913   if (AIsAlloc != BIsAlloc)
914     return AIsAlloc;
915 
916   // We don't have any special requirements for the relative order of
917   // two non allocatable sections.
918   if (!AIsAlloc)
919     return false;
920 
921   // We want the read only sections first so that they go in the PT_LOAD
922   // covering the program headers at the start of the file.
923   bool AIsWritable = AFlags & SHF_WRITE;
924   bool BIsWritable = BFlags & SHF_WRITE;
925   if (AIsWritable != BIsWritable)
926     return BIsWritable;
927 
928   // For a corresponding reason, put non exec sections first (the program
929   // header PT_LOAD is not executable).
930   bool AIsExec = AFlags & SHF_EXECINSTR;
931   bool BIsExec = BFlags & SHF_EXECINSTR;
932   if (AIsExec != BIsExec)
933     return BIsExec;
934 
935   // If we got here we know that both A and B are in the same PT_LOAD.
936 
937   // The TLS initialization block needs to be a single contiguous block in a R/W
938   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
939   // sections are placed here as they don't take up virtual address space in the
940   // PT_LOAD.
941   bool AIsTls = AFlags & SHF_TLS;
942   bool BIsTls = BFlags & SHF_TLS;
943   if (AIsTls != BIsTls)
944     return AIsTls;
945 
946   // The next requirement we have is to put nobits sections last. The
947   // reason is that the only thing the dynamic linker will see about
948   // them is a p_memsz that is larger than p_filesz. Seeing that it
949   // zeros the end of the PT_LOAD, so that has to correspond to the
950   // nobits sections.
951   bool AIsNoBits = A->getType() == SHT_NOBITS;
952   bool BIsNoBits = B->getType() == SHT_NOBITS;
953   if (AIsNoBits != BIsNoBits)
954     return BIsNoBits;
955 
956   // We place RelRo section before plain r/w ones.
957   bool AIsRelRo = isRelroSection(A);
958   bool BIsRelRo = isRelroSection(B);
959   if (AIsRelRo != BIsRelRo)
960     return AIsRelRo;
961 
962   // Some architectures have additional ordering restrictions for sections
963   // within the same PT_LOAD.
964   if (Config->EMachine == EM_PPC64)
965     return getPPC64SectionRank(A->getName()) <
966            getPPC64SectionRank(B->getName());
967 
968   return false;
969 }
970 
971 // The .bss section does not exist if no input file has a .bss section.
972 // This function creates one if that's the case.
973 template <class ELFT> void Writer<ELFT>::ensureBss() {
974   if (Out<ELFT>::Bss)
975     return;
976   Out<ELFT>::Bss =
977       new OutputSection<ELFT>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
978   OwningSections.emplace_back(Out<ELFT>::Bss);
979   OutputSections.push_back(Out<ELFT>::Bss);
980 }
981 
982 // Until this function is called, common symbols do not belong to any section.
983 // This function adds them to end of BSS section.
984 template <class ELFT>
985 void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
986   if (Syms.empty())
987     return;
988 
989   // Sort the common symbols by alignment as an heuristic to pack them better.
990   std::stable_sort(Syms.begin(), Syms.end(),
991                    [](const DefinedCommon *A, const DefinedCommon *B) {
992                      return A->Alignment > B->Alignment;
993                    });
994 
995   ensureBss();
996   uintX_t Off = Out<ELFT>::Bss->getSize();
997   for (DefinedCommon *C : Syms) {
998     Off = alignTo(Off, C->Alignment);
999     Out<ELFT>::Bss->updateAlign(C->Alignment);
1000     C->OffsetInBss = Off;
1001     Off += C->Size;
1002   }
1003 
1004   Out<ELFT>::Bss->setSize(Off);
1005 }
1006 
1007 template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
1008   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1009 
1010   uintX_t SecAlign = SS->File->getSection(SS->Sym)->sh_addralign;
1011   uintX_t SymValue = SS->Sym.st_value;
1012   int TrailingZeros =
1013       std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
1014   return 1 << TrailingZeros;
1015 }
1016 
1017 // Reserve space in .bss for copy relocation.
1018 template <class ELFT>
1019 void Writer<ELFT>::addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
1020   ensureBss();
1021   uintX_t Align = getAlignment(SS);
1022   uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Align);
1023   Out<ELFT>::Bss->setSize(Off + SS->template getSize<ELFT>());
1024   Out<ELFT>::Bss->updateAlign(Align);
1025   uintX_t Shndx = SS->Sym.st_shndx;
1026   uintX_t Value = SS->Sym.st_value;
1027   // Look through the DSO's dynamic symbol for aliases and create a dynamic
1028   // symbol for each one. This causes the copy relocation to correctly interpose
1029   // any aliases.
1030   for (SharedSymbol<ELFT> &S : SS->File->getSharedSymbols()) {
1031     if (S.Sym.st_shndx != Shndx || S.Sym.st_value != Value)
1032       continue;
1033     S.OffsetInBss = Off;
1034     S.NeedsCopyOrPltAddr = true;
1035     S.Backref->IsUsedInRegularObj = true;
1036   }
1037   Out<ELFT>::RelaDyn->addReloc(
1038       {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
1039 }
1040 
1041 template <class ELFT>
1042 StringRef Writer<ELFT>::getOutputSectionName(InputSectionBase<ELFT> *S) const {
1043   StringRef Dest = Script<ELFT>::X->getOutputSection(S);
1044   if (!Dest.empty())
1045     return Dest;
1046 
1047   StringRef Name = S->getSectionName();
1048   for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
1049                       ".init_array.", ".fini_array.", ".ctors.", ".dtors.",
1050                       ".tbss.", ".gcc_except_table.", ".tdata."})
1051     if (Name.startswith(V))
1052       return V.drop_back();
1053   return Name;
1054 }
1055 
1056 template <class ELFT>
1057 void reportDiscarded(InputSectionBase<ELFT> *IS,
1058                      const std::unique_ptr<elf::ObjectFile<ELFT>> &File) {
1059   if (!Config->PrintGcSections || !IS || IS->Live)
1060     return;
1061   llvm::errs() << "removing unused section from '" << IS->getSectionName()
1062                << "' in file '" << File->getName() << "'\n";
1063 }
1064 
1065 template <class ELFT>
1066 bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const {
1067   return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
1068          Script<ELFT>::X->isDiscarded(S);
1069 }
1070 
1071 template <class ELFT>
1072 static SymbolBody *
1073 addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name,
1074                      OutputSectionBase<ELFT> &Sec, typename ELFT::uint Val) {
1075   if (!Table.find(Name))
1076     return nullptr;
1077   return Table.addSynthetic(Name, Sec, Val);
1078 }
1079 
1080 // The beginning and the ending of .rel[a].plt section are marked
1081 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
1082 // executable. The runtime needs these symbols in order to resolve
1083 // all IRELATIVE relocs on startup. For dynamic executables, we don't
1084 // need these symbols, since IRELATIVE relocs are resolved through GOT
1085 // and PLT. For details, see http://www.airs.com/blog/archives/403.
1086 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1087   if (isOutputDynamic() || !Out<ELFT>::RelaPlt)
1088     return;
1089   StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
1090   ElfSym<ELFT>::RelaIpltStart =
1091       addOptionalSynthetic(Symtab, S, *Out<ELFT>::RelaPlt, 0);
1092 
1093   S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
1094   ElfSym<ELFT>::RelaIpltEnd = addOptionalSynthetic(
1095       Symtab, S, *Out<ELFT>::RelaPlt, DefinedSynthetic<ELFT>::SectionEnd);
1096 }
1097 
1098 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
1099   if (!B.Backref->IsUsedInRegularObj)
1100     return false;
1101 
1102   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
1103     // Exclude symbols pointing to garbage-collected sections.
1104     if (D->Section && !D->Section->Live)
1105       return false;
1106   }
1107   return true;
1108 }
1109 
1110 // This class knows how to create an output section for a given
1111 // input section. Output section type is determined by various
1112 // factors, including input section's sh_flags, sh_type and
1113 // linker scripts.
1114 namespace {
1115 template <class ELFT> class OutputSectionFactory {
1116   typedef typename ELFT::Shdr Elf_Shdr;
1117   typedef typename ELFT::uint uintX_t;
1118 
1119 public:
1120   std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
1121                                                     StringRef OutsecName);
1122 
1123   OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type,
1124                                   uintX_t Flags) {
1125     return Map.lookup({Name, Type, Flags, 0});
1126   }
1127 
1128 private:
1129   SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1130                                        StringRef OutsecName);
1131 
1132   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map;
1133 };
1134 }
1135 
1136 template <class ELFT>
1137 std::pair<OutputSectionBase<ELFT> *, bool>
1138 OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1139                                    StringRef OutsecName) {
1140   SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
1141   OutputSectionBase<ELFT> *&Sec = Map[Key];
1142   if (Sec)
1143     return {Sec, false};
1144 
1145   switch (C->SectionKind) {
1146   case InputSectionBase<ELFT>::Regular:
1147     Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
1148     break;
1149   case InputSectionBase<ELFT>::EHFrame:
1150     Sec = new EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
1151     break;
1152   case InputSectionBase<ELFT>::Merge:
1153     Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags,
1154                                        Key.Alignment);
1155     break;
1156   case InputSectionBase<ELFT>::MipsReginfo:
1157     Sec = new MipsReginfoOutputSection<ELFT>();
1158     break;
1159   }
1160   return {Sec, true};
1161 }
1162 
1163 template <class ELFT>
1164 SectionKey<ELFT::Is64Bits>
1165 OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C,
1166                                       StringRef OutsecName) {
1167   const Elf_Shdr *H = C->getSectionHdr();
1168   uintX_t Flags = H->sh_flags & ~SHF_GROUP;
1169 
1170   // For SHF_MERGE we create different output sections for each alignment.
1171   // This makes each output section simple and keeps a single level mapping from
1172   // input to output.
1173   uintX_t Alignment = 0;
1174   if (isa<MergeInputSection<ELFT>>(C))
1175     Alignment = std::max(H->sh_addralign, H->sh_entsize);
1176 
1177   // GNU as can give .eh_frame section type SHT_PROGBITS or SHT_X86_64_UNWIND
1178   // depending on the construct. We want to canonicalize it so that
1179   // there is only one .eh_frame in the end.
1180   uint32_t Type = H->sh_type;
1181   if (Type == SHT_PROGBITS && Config->EMachine == EM_X86_64 &&
1182       isa<EHInputSection<ELFT>>(C))
1183     Type = SHT_X86_64_UNWIND;
1184 
1185   return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment};
1186 }
1187 
1188 // The linker is expected to define some symbols depending on
1189 // the linking result. This function defines such symbols.
1190 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
1191   if (Config->EMachine == EM_MIPS) {
1192     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
1193     // so that it points to an absolute address which is relative to GOT.
1194     // See "Global Data Symbols" in Chapter 6 in the following document:
1195     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1196     ElfSym<ELFT>::MipsGp =
1197         Symtab.addSynthetic("_gp", *Out<ELFT>::Got, MipsGPOffset);
1198 
1199     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
1200     // start of function and 'gp' pointer into GOT.
1201     ElfSym<ELFT>::MipsGpDisp =
1202         addOptionalSynthetic(Symtab, "_gp_disp", *Out<ELFT>::Got, MipsGPOffset);
1203     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
1204     // pointer. This symbol is used in the code generated by .cpload pseudo-op
1205     // in case of using -mno-shared option.
1206     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
1207     ElfSym<ELFT>::MipsLocalGp = addOptionalSynthetic(
1208         Symtab, "__gnu_local_gp", *Out<ELFT>::Got, MipsGPOffset);
1209   }
1210 
1211   // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
1212   // is magical and is used to produce a R_386_GOTPC relocation.
1213   // The R_386_GOTPC relocation value doesn't actually depend on the
1214   // symbol value, so it could use an index of STN_UNDEF which, according
1215   // to the spec, means the symbol value is 0.
1216   // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
1217   // the object file.
1218   // The situation is even stranger on x86_64 where the assembly doesn't
1219   // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
1220   // an undefined symbol in the .o files.
1221   // Given that the symbol is effectively unused, we just create a dummy
1222   // hidden one to avoid the undefined symbol error.
1223   if (!Config->Relocatable)
1224     Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
1225 
1226   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
1227   // static linking the linker is required to optimize away any references to
1228   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
1229   // to avoid the undefined symbol error.
1230   if (!isOutputDynamic())
1231     Symtab.addIgnored("__tls_get_addr");
1232 
1233   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
1234                        DefinedRegular<ELFT> *&Sym2) {
1235     Sym1 = Symtab.addIgnored(S, STV_DEFAULT);
1236 
1237     // The name without the underscore is not a reserved name,
1238     // so it is defined only when there is a reference against it.
1239     assert(S.startswith("_"));
1240     S = S.substr(1);
1241     if (SymbolBody *B = Symtab.find(S))
1242       if (B->isUndefined())
1243         Sym2 = Symtab.addAbsolute(S, STV_DEFAULT);
1244   };
1245 
1246   Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
1247   Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2);
1248   Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2);
1249 }
1250 
1251 // Sort input sections by section name suffixes for
1252 // __attribute__((init_priority(N))).
1253 template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) {
1254   if (S)
1255     reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
1256 }
1257 
1258 // Sort input sections by the special rule for .ctors and .dtors.
1259 template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
1260   if (S)
1261     reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
1262 }
1263 
1264 // Create output section objects and add them to OutputSections.
1265 template <class ELFT> void Writer<ELFT>::createSections() {
1266   // Add .interp first because some loaders want to see that section
1267   // on the first page of the executable file when loaded into memory.
1268   if (needsInterpSection())
1269     OutputSections.push_back(Out<ELFT>::Interp);
1270 
1271   // A core file does not usually contain unmodified segments except
1272   // the first page of the executable. Add the build ID section now
1273   // so that the section is included in the first page.
1274   if (Out<ELFT>::BuildId)
1275     OutputSections.push_back(Out<ELFT>::BuildId);
1276 
1277   // Create output sections for input object file sections.
1278   std::vector<OutputSectionBase<ELFT> *> RegularSections;
1279   OutputSectionFactory<ELFT> Factory;
1280   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
1281        Symtab.getObjectFiles()) {
1282     for (InputSectionBase<ELFT> *C : F->getSections()) {
1283       if (isDiscarded(C)) {
1284         reportDiscarded(C, F);
1285         continue;
1286       }
1287       OutputSectionBase<ELFT> *Sec;
1288       bool IsNew;
1289       std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C));
1290       if (IsNew) {
1291         OwningSections.emplace_back(Sec);
1292         OutputSections.push_back(Sec);
1293         RegularSections.push_back(Sec);
1294       }
1295       Sec->addSection(C);
1296     }
1297   }
1298 
1299   Out<ELFT>::Bss = static_cast<OutputSection<ELFT> *>(
1300       Factory.lookup(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE));
1301 
1302   // If we have a .opd section (used under PPC64 for function descriptors),
1303   // store a pointer to it here so that we can use it later when processing
1304   // relocations.
1305   Out<ELFT>::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC);
1306 
1307   Out<ELFT>::Dynamic->PreInitArraySec = Factory.lookup(
1308       ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC);
1309   Out<ELFT>::Dynamic->InitArraySec =
1310       Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC);
1311   Out<ELFT>::Dynamic->FiniArraySec =
1312       Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC);
1313 
1314   // Sort section contents for __attribute__((init_priority(N)).
1315   sortInitFini(Out<ELFT>::Dynamic->InitArraySec);
1316   sortInitFini(Out<ELFT>::Dynamic->FiniArraySec);
1317   sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1318   sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1319 
1320   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1321   // symbols for sections, so that the runtime can get the start and end
1322   // addresses of each section by section name. Add such symbols.
1323   if (!Config->Relocatable) {
1324     addStartEndSymbols();
1325     for (OutputSectionBase<ELFT> *Sec : RegularSections)
1326       addStartStopSymbols(Sec);
1327   }
1328 
1329   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1330   // It should be okay as no one seems to care about the type.
1331   // Even the author of gold doesn't remember why gold behaves that way.
1332   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1333   if (isOutputDynamic())
1334     Symtab.addSynthetic("_DYNAMIC", *Out<ELFT>::Dynamic, 0);
1335 
1336   // Define __rel[a]_iplt_{start,end} symbols if needed.
1337   addRelIpltSymbols();
1338 
1339   if (Out<ELFT>::EhFrameHdr->Sec)
1340     Out<ELFT>::EhFrameHdr->Sec->finalize();
1341 
1342   // Scan relocations. This must be done after every symbol is declared so that
1343   // we can correctly decide if a dynamic relocation is needed.
1344   // Check size() each time to guard against .bss being created.
1345   for (unsigned I = 0; I < OutputSections.size(); ++I) {
1346     OutputSectionBase<ELFT> *Sec = OutputSections[I];
1347     Sec->forEachInputSection([&](InputSectionBase<ELFT> *S) {
1348       if (auto *IS = dyn_cast<InputSection<ELFT>>(S)) {
1349         // Set OutSecOff so that scanRelocs can use it.
1350         uintX_t Off = alignTo(Sec->getSize(), S->Align);
1351         IS->OutSecOff = Off;
1352 
1353         scanRelocs(*IS);
1354 
1355         // Now that scan relocs possibly changed the size, update the offset.
1356         Sec->setSize(Off + S->getSize());
1357       } else if (auto *EH = dyn_cast<EHInputSection<ELFT>>(S)) {
1358         if (EH->RelocSection)
1359           scanRelocs(*EH, *EH->RelocSection);
1360       }
1361     });
1362   }
1363 
1364   // Now that we have defined all possible symbols including linker-
1365   // synthesized ones. Visit all symbols to give the finishing touches.
1366   std::vector<DefinedCommon *> CommonSymbols;
1367   for (Symbol *S : Symtab.getSymbols()) {
1368     SymbolBody *Body = S->Body;
1369 
1370     // Set "used" bit for --as-needed.
1371     if (S->IsUsedInRegularObj && !S->isWeak())
1372       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
1373         SS->File->IsUsed = true;
1374 
1375     if (Body->isUndefined() && !S->isWeak())
1376       reportUndefined<ELFT>(Symtab, Body);
1377 
1378     if (auto *C = dyn_cast<DefinedCommon>(Body))
1379       CommonSymbols.push_back(C);
1380 
1381     if (!includeInSymtab<ELFT>(*Body))
1382       continue;
1383     if (Out<ELFT>::SymTab)
1384       Out<ELFT>::SymTab->addSymbol(Body);
1385 
1386     if (isOutputDynamic() && S->includeInDynsym()) {
1387       Out<ELFT>::DynSymTab->addSymbol(Body);
1388       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
1389         Out<ELFT>::VerNeed->addSymbol(SS);
1390     }
1391   }
1392 
1393   // Do not proceed if there was an undefined symbol.
1394   if (HasError)
1395     return;
1396 
1397   addCommonSymbols(CommonSymbols);
1398 
1399   // So far we have added sections from input object files.
1400   // This function adds linker-created Out<ELFT>::* sections.
1401   addPredefinedSections();
1402 
1403   std::stable_sort(OutputSections.begin(), OutputSections.end(),
1404                    compareSections<ELFT>);
1405 
1406   unsigned I = 1;
1407   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1408     Sec->SectionIndex = I++;
1409     Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName()));
1410   }
1411 
1412   // Finalizers fix each section's size.
1413   // .dynsym is finalized early since that may fill up .gnu.hash.
1414   if (isOutputDynamic())
1415     Out<ELFT>::DynSymTab->finalize();
1416 
1417   // Fill other section headers. The dynamic table is finalized
1418   // at the end because some tags like RELSZ depend on result
1419   // of finalizing other sections. The dynamic string table is
1420   // finalized once the .dynamic finalizer has added a few last
1421   // strings. See DynamicSection::finalize()
1422   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1423     if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic)
1424       Sec->finalize();
1425 
1426   if (isOutputDynamic())
1427     Out<ELFT>::Dynamic->finalize();
1428 }
1429 
1430 template <class ELFT> bool Writer<ELFT>::needsGot() {
1431   if (!Out<ELFT>::Got->empty())
1432     return true;
1433 
1434   // We add the .got section to the result for dynamic MIPS target because
1435   // its address and properties are mentioned in the .dynamic section.
1436   if (Config->EMachine == EM_MIPS)
1437     return true;
1438 
1439   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
1440   // we need to emit a GOT even if it's empty.
1441   return HasGotOffRel;
1442 }
1443 
1444 // This function add Out<ELFT>::* sections to OutputSections.
1445 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1446   auto Add = [&](OutputSectionBase<ELFT> *C) {
1447     if (C)
1448       OutputSections.push_back(C);
1449   };
1450 
1451   // This order is not the same as the final output order
1452   // because we sort the sections using their attributes below.
1453   Add(Out<ELFT>::SymTab);
1454   Add(Out<ELFT>::ShStrTab);
1455   Add(Out<ELFT>::StrTab);
1456   if (isOutputDynamic()) {
1457     Add(Out<ELFT>::DynSymTab);
1458     if (Out<ELFT>::VerNeed->getNeedNum() != 0) {
1459       Add(Out<ELFT>::VerSym);
1460       Add(Out<ELFT>::VerNeed);
1461     }
1462     Add(Out<ELFT>::GnuHashTab);
1463     Add(Out<ELFT>::HashTab);
1464     Add(Out<ELFT>::Dynamic);
1465     Add(Out<ELFT>::DynStrTab);
1466     if (Out<ELFT>::RelaDyn->hasRelocs())
1467       Add(Out<ELFT>::RelaDyn);
1468     Add(Out<ELFT>::MipsRldMap);
1469   }
1470 
1471   // We always need to add rel[a].plt to output if it has entries.
1472   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
1473   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
1474     Add(Out<ELFT>::RelaPlt);
1475     Out<ELFT>::RelaPlt->Static = !isOutputDynamic();
1476   }
1477 
1478   if (needsGot())
1479     Add(Out<ELFT>::Got);
1480   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
1481     Add(Out<ELFT>::GotPlt);
1482   if (!Out<ELFT>::Plt->empty())
1483     Add(Out<ELFT>::Plt);
1484   if (Out<ELFT>::EhFrameHdr->Live)
1485     Add(Out<ELFT>::EhFrameHdr);
1486 }
1487 
1488 // The linker is expected to define SECNAME_start and SECNAME_end
1489 // symbols for a few sections. This function defines them.
1490 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1491   auto Define = [&](StringRef Start, StringRef End,
1492                     OutputSectionBase<ELFT> *OS) {
1493     if (OS) {
1494       Symtab.addSynthetic(Start, *OS, 0);
1495       Symtab.addSynthetic(End, *OS, DefinedSynthetic<ELFT>::SectionEnd);
1496     } else {
1497       Symtab.addIgnored(Start);
1498       Symtab.addIgnored(End);
1499     }
1500   };
1501 
1502   Define("__preinit_array_start", "__preinit_array_end",
1503          Out<ELFT>::Dynamic->PreInitArraySec);
1504   Define("__init_array_start", "__init_array_end",
1505          Out<ELFT>::Dynamic->InitArraySec);
1506   Define("__fini_array_start", "__fini_array_end",
1507          Out<ELFT>::Dynamic->FiniArraySec);
1508 }
1509 
1510 // If a section name is valid as a C identifier (which is rare because of
1511 // the leading '.'), linkers are expected to define __start_<secname> and
1512 // __stop_<secname> symbols. They are at beginning and end of the section,
1513 // respectively. This is not requested by the ELF standard, but GNU ld and
1514 // gold provide the feature, and used by many programs.
1515 template <class ELFT>
1516 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
1517   StringRef S = Sec->getName();
1518   if (!isValidCIdentifier(S))
1519     return;
1520   StringSaver Saver(Alloc);
1521   StringRef Start = Saver.save("__start_" + S);
1522   StringRef Stop = Saver.save("__stop_" + S);
1523   if (SymbolBody *B = Symtab.find(Start))
1524     if (B->isUndefined())
1525       Symtab.addSynthetic(Start, *Sec, 0);
1526   if (SymbolBody *B = Symtab.find(Stop))
1527     if (B->isUndefined())
1528       Symtab.addSynthetic(Stop, *Sec, DefinedSynthetic<ELFT>::SectionEnd);
1529 }
1530 
1531 template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) {
1532   if (!(Sec->getFlags() & SHF_ALLOC))
1533     return false;
1534 
1535   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1536   // responsible for allocating space for them, not the PT_LOAD that
1537   // contains the TLS initialization image.
1538   if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS)
1539     return false;
1540   return true;
1541 }
1542 
1543 static uint32_t toPhdrFlags(uint64_t Flags) {
1544   uint32_t Ret = PF_R;
1545   if (Flags & SHF_WRITE)
1546     Ret |= PF_W;
1547   if (Flags & SHF_EXECINSTR)
1548     Ret |= PF_X;
1549   return Ret;
1550 }
1551 
1552 // Decide which program headers to create and which sections to include in each
1553 // one.
1554 template <class ELFT> void Writer<ELFT>::createPhdrs() {
1555   auto AddHdr = [this](unsigned Type, unsigned Flags) {
1556     return &*Phdrs.emplace(Phdrs.end(), Type, Flags);
1557   };
1558 
1559   auto AddSec = [](Phdr &Hdr, OutputSectionBase<ELFT> *Sec) {
1560     Hdr.Last = Sec;
1561     if (!Hdr.First)
1562       Hdr.First = Sec;
1563     Hdr.H.p_align = std::max<uintX_t>(Hdr.H.p_align, Sec->getAlign());
1564   };
1565 
1566   // The first phdr entry is PT_PHDR which describes the program header itself.
1567   Phdr &Hdr = *AddHdr(PT_PHDR, PF_R);
1568   AddSec(Hdr, Out<ELFT>::ProgramHeaders);
1569 
1570   // PT_INTERP must be the second entry if exists.
1571   if (needsInterpSection()) {
1572     Phdr &Hdr = *AddHdr(PT_INTERP, toPhdrFlags(Out<ELFT>::Interp->getFlags()));
1573     AddSec(Hdr, Out<ELFT>::Interp);
1574   }
1575 
1576   // Add the first PT_LOAD segment for regular output sections.
1577   uintX_t Flags = PF_R;
1578   Phdr *Load = AddHdr(PT_LOAD, Flags);
1579   AddSec(*Load, Out<ELFT>::ElfHeader);
1580   AddSec(*Load, Out<ELFT>::ProgramHeaders);
1581 
1582   Phdr TlsHdr(PT_TLS, PF_R);
1583   Phdr RelRo(PT_GNU_RELRO, PF_R);
1584   Phdr Note(PT_NOTE, PF_R);
1585   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1586     if (!(Sec->getFlags() & SHF_ALLOC))
1587       break;
1588 
1589     // If we meet TLS section then we create TLS header
1590     // and put all TLS sections inside for futher use when
1591     // assign addresses.
1592     if (Sec->getFlags() & SHF_TLS)
1593       AddSec(TlsHdr, Sec);
1594 
1595     if (!needsPtLoad<ELFT>(Sec))
1596       continue;
1597 
1598     // If flags changed then we want new load segment.
1599     uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
1600     if (Flags != NewFlags) {
1601       Load = AddHdr(PT_LOAD, NewFlags);
1602       Flags = NewFlags;
1603     }
1604 
1605     AddSec(*Load, Sec);
1606 
1607     if (isRelroSection(Sec))
1608       AddSec(RelRo, Sec);
1609     if (Sec->getType() == SHT_NOTE)
1610       AddSec(Note, Sec);
1611   }
1612 
1613   // Add the TLS segment unless it's empty.
1614   if (TlsHdr.First)
1615     Phdrs.push_back(std::move(TlsHdr));
1616 
1617   // Add an entry for .dynamic.
1618   if (isOutputDynamic()) {
1619     Phdr &H = *AddHdr(PT_DYNAMIC, toPhdrFlags(Out<ELFT>::Dynamic->getFlags()));
1620     AddSec(H, Out<ELFT>::Dynamic);
1621   }
1622 
1623   // PT_GNU_RELRO includes all sections that should be marked as
1624   // read-only by dynamic linker after proccessing relocations.
1625   if (RelRo.First)
1626     Phdrs.push_back(std::move(RelRo));
1627 
1628   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1629   if (Out<ELFT>::EhFrameHdr->Live) {
1630     Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME,
1631                         toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()));
1632     AddSec(Hdr, Out<ELFT>::EhFrameHdr);
1633   }
1634 
1635   // PT_GNU_STACK is a special section to tell the loader to make the
1636   // pages for the stack non-executable.
1637   if (!Config->ZExecStack)
1638     AddHdr(PT_GNU_STACK, PF_R | PF_W);
1639 
1640   if (Note.First)
1641     Phdrs.push_back(std::move(Note));
1642 
1643   Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size());
1644 }
1645 
1646 // The first section of each PT_LOAD and the first section after PT_GNU_RELRO
1647 // have to be page aligned so that the dynamic linker can set the permissions.
1648 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1649   for (const Phdr &P : Phdrs)
1650     if (P.H.p_type == PT_LOAD)
1651       P.First->PageAlign = true;
1652 
1653   for (const Phdr &P : Phdrs) {
1654     if (P.H.p_type != PT_GNU_RELRO)
1655       continue;
1656     // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1657     // have to align it to a page.
1658     auto End = OutputSections.end();
1659     auto I = std::find(OutputSections.begin(), End, P.Last);
1660     if (I == End || (I + 1) == End)
1661       continue;
1662     OutputSectionBase<ELFT> *Sec = *(I + 1);
1663     if (needsPtLoad(Sec))
1664       Sec->PageAlign = true;
1665   }
1666 }
1667 
1668 // We should set file offsets and VAs for elf header and program headers
1669 // sections. These are special, we do not include them into output sections
1670 // list, but have them to simplify the code.
1671 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1672   uintX_t BaseVA = ScriptConfig->DoLayout ? 0 : Target->getVAStart();
1673   Out<ELFT>::ElfHeader->setVA(BaseVA);
1674   Out<ELFT>::ElfHeader->setFileOffset(0);
1675   uintX_t Off = Out<ELFT>::ElfHeader->getSize();
1676   Out<ELFT>::ProgramHeaders->setVA(Off + BaseVA);
1677   Out<ELFT>::ProgramHeaders->setFileOffset(Off);
1678 }
1679 
1680 // Assign VAs (addresses at run-time) to output sections.
1681 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1682   uintX_t VA = Target->getVAStart() + Out<ELFT>::ElfHeader->getSize() +
1683                Out<ELFT>::ProgramHeaders->getSize();
1684 
1685   uintX_t ThreadBssOffset = 0;
1686   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1687     uintX_t Align = Sec->getAlign();
1688     if (Sec->PageAlign)
1689       Align = std::max<uintX_t>(Align, Target->PageSize);
1690 
1691     // We only assign VAs to allocated sections.
1692     if (needsPtLoad<ELFT>(Sec)) {
1693       VA = alignTo(VA, Align);
1694       Sec->setVA(VA);
1695       VA += Sec->getSize();
1696     } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) {
1697       uintX_t TVA = VA + ThreadBssOffset;
1698       TVA = alignTo(TVA, Align);
1699       Sec->setVA(TVA);
1700       ThreadBssOffset = TVA - VA + Sec->getSize();
1701     }
1702   }
1703 }
1704 
1705 // Adjusts the file alignment for a given output section and returns
1706 // its new file offset. The file offset must be the same with its
1707 // virtual address (modulo the page size) so that the loader can load
1708 // executables without any address adjustment.
1709 template <class ELFT, class uintX_t>
1710 static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) {
1711   uintX_t Align = Sec->getAlign();
1712   if (Sec->PageAlign)
1713     Align = std::max<uintX_t>(Align, Target->PageSize);
1714   Off = alignTo(Off, Align);
1715 
1716   // Relocatable output does not have program headers
1717   // and does not need any other offset adjusting.
1718   if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC))
1719     return Off;
1720   return alignTo(Off, Target->PageSize, Sec->getVA());
1721 }
1722 
1723 // Assign file offsets to output sections.
1724 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1725   uintX_t Off =
1726       Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
1727 
1728   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1729     if (Sec->getType() == SHT_NOBITS) {
1730       Sec->setFileOffset(Off);
1731       continue;
1732     }
1733 
1734     Off = getFileAlignment<ELFT>(Off, Sec);
1735     Sec->setFileOffset(Off);
1736     Off += Sec->getSize();
1737   }
1738   SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
1739   FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1740 }
1741 
1742 // Finalize the program headers. We call this function after we assign
1743 // file offsets and VAs to all sections.
1744 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1745   for (Phdr &P : Phdrs) {
1746     Elf_Phdr &H = P.H;
1747     OutputSectionBase<ELFT> *First = P.First;
1748     OutputSectionBase<ELFT> *Last = P.Last;
1749     if (First) {
1750       H.p_filesz = Last->getFileOff() - First->getFileOff();
1751       if (Last->getType() != SHT_NOBITS)
1752         H.p_filesz += Last->getSize();
1753       H.p_memsz = Last->getVA() + Last->getSize() - First->getVA();
1754       H.p_offset = First->getFileOff();
1755       H.p_vaddr = First->getVA();
1756     }
1757     if (H.p_type == PT_LOAD)
1758       H.p_align = Target->PageSize;
1759     else if (H.p_type == PT_GNU_RELRO)
1760       H.p_align = 1;
1761     H.p_paddr = H.p_vaddr;
1762 
1763     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1764     // so round up the size to make sure the offsets are correct.
1765     if (H.p_type == PT_TLS) {
1766       Out<ELFT>::TlsPhdr = &H;
1767       H.p_memsz = alignTo(H.p_memsz, H.p_align);
1768     }
1769   }
1770 }
1771 
1772 static uint32_t getMipsEFlags() {
1773   // FIXME: In fact ELF flags depends on ELF flags of input object files
1774   // and selected emulation. For now just use hard coded values.
1775   uint32_t V = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2;
1776   if (Config->Shared)
1777     V |= EF_MIPS_PIC;
1778   return V;
1779 }
1780 
1781 template <class ELFT> static typename ELFT::uint getEntryAddr() {
1782   if (Symbol *S = Config->EntrySym)
1783     return S->Body->getVA<ELFT>();
1784   if (Config->EntryAddr != uint64_t(-1))
1785     return Config->EntryAddr;
1786   return 0;
1787 }
1788 
1789 template <class ELFT> static uint8_t getELFEncoding() {
1790   if (ELFT::TargetEndianness == llvm::support::little)
1791     return ELFDATA2LSB;
1792   return ELFDATA2MSB;
1793 }
1794 
1795 static uint16_t getELFType() {
1796   if (Config->Pic)
1797     return ET_DYN;
1798   if (Config->Relocatable)
1799     return ET_REL;
1800   return ET_EXEC;
1801 }
1802 
1803 // This function is called after we have assigned address and size
1804 // to each section. This function fixes some predefined absolute
1805 // symbol values that depend on section address and size.
1806 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1807   auto Set = [](DefinedRegular<ELFT> *&S1, DefinedRegular<ELFT> *&S2,
1808                 uintX_t V) {
1809     if (S1)
1810       S1->Value = V;
1811     if (S2)
1812       S2->Value = V;
1813   };
1814 
1815   // _etext is the first location after the last read-only loadable segment.
1816   // _edata is the first location after the last read-write loadable segment.
1817   // _end is the first location after the uninitialized data region.
1818   for (Phdr &P : Phdrs) {
1819     Elf_Phdr &H = P.H;
1820     if (H.p_type != PT_LOAD)
1821       continue;
1822     Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, H.p_vaddr + H.p_memsz);
1823 
1824     uintX_t Val = H.p_vaddr + H.p_filesz;
1825     if (H.p_flags & PF_W)
1826       Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val);
1827     else
1828       Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val);
1829   }
1830 }
1831 
1832 template <class ELFT> void Writer<ELFT>::writeHeader() {
1833   uint8_t *Buf = Buffer->getBufferStart();
1834   memcpy(Buf, "\177ELF", 4);
1835 
1836   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1837 
1838   // Write the ELF header.
1839   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1840   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1841   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1842   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1843   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1844   EHdr->e_type = getELFType();
1845   EHdr->e_machine = FirstObj.getEMachine();
1846   EHdr->e_version = EV_CURRENT;
1847   EHdr->e_entry = getEntryAddr<ELFT>();
1848   EHdr->e_shoff = SectionHeaderOff;
1849   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1850   EHdr->e_phnum = Phdrs.size();
1851   EHdr->e_shentsize = sizeof(Elf_Shdr);
1852   EHdr->e_shnum = OutputSections.size() + 1;
1853   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1854 
1855   if (Config->EMachine == EM_MIPS)
1856     EHdr->e_flags = getMipsEFlags();
1857 
1858   if (!Config->Relocatable) {
1859     EHdr->e_phoff = sizeof(Elf_Ehdr);
1860     EHdr->e_phentsize = sizeof(Elf_Phdr);
1861   }
1862 
1863   // Write the program header table.
1864   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1865   for (Phdr &P : Phdrs)
1866     *HBuf++ = P.H;
1867 
1868   // Write the section header table. Note that the first table entry is null.
1869   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1870   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1871     Sec->writeHeaderTo(++SHdrs);
1872 }
1873 
1874 template <class ELFT> void Writer<ELFT>::openFile() {
1875   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1876       FileOutputBuffer::create(Config->OutputFile, FileSize,
1877                                FileOutputBuffer::F_executable);
1878   if (BufferOrErr)
1879     Buffer = std::move(*BufferOrErr);
1880   else
1881     error(BufferOrErr, "failed to open " + Config->OutputFile);
1882 }
1883 
1884 // Write section contents to a mmap'ed file.
1885 template <class ELFT> void Writer<ELFT>::writeSections() {
1886   uint8_t *Buf = Buffer->getBufferStart();
1887 
1888   // PPC64 needs to process relocations in the .opd section before processing
1889   // relocations in code-containing sections.
1890   if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) {
1891     Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
1892     Sec->writeTo(Buf + Sec->getFileOff());
1893   }
1894 
1895   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1896     if (Sec != Out<ELFT>::Opd)
1897       Sec->writeTo(Buf + Sec->getFileOff());
1898 }
1899 
1900 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1901   BuildIdSection<ELFT> *S = Out<ELFT>::BuildId;
1902   if (!S)
1903     return;
1904 
1905   // Compute a hash of all sections except .debug_* sections.
1906   // We skip debug sections because they tend to be very large
1907   // and their contents are very likely to be the same as long as
1908   // other sections are the same.
1909   uint8_t *Start = Buffer->getBufferStart();
1910   uint8_t *Last = Start;
1911   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1912     uint8_t *End = Start + Sec->getFileOff();
1913     if (!Sec->getName().startswith(".debug_"))
1914       S->update({Last, End});
1915     Last = End;
1916   }
1917   S->update({Last, Start + FileSize});
1918 
1919   // Fill the hash value field in the .note.gnu.build-id section.
1920   S->writeBuildId();
1921 }
1922 
1923 template void elf::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
1924 template void elf::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
1925 template void elf::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
1926 template void elf::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
1927