xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision bc32b5cf)
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->usesOnlyLowPageBits(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 IsWrite = Flags & SHF_WRITE;
536 
537   auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
538     Out<ELFT>::RelaDyn->addReloc(Reloc);
539   };
540 
541   const elf::ObjectFile<ELFT> &File = *C.getFile();
542   ArrayRef<uint8_t> SectionData = C.getSectionData();
543   const uint8_t *Buf = SectionData.begin();
544   for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
545     const RelTy &RI = *I;
546     SymbolBody &Body = File.getRelocTargetSym(RI);
547     uint32_t Type = RI.getType(Config->Mips64EL);
548 
549     // Ignore "hint" relocation because it is for optional code optimization.
550     if (Target->isHintRel(Type))
551       continue;
552 
553     uintX_t Offset = C.getOffset(RI.r_offset);
554     if (Offset == (uintX_t)-1)
555       continue;
556 
557     RelExpr Expr = Target->getRelExpr(Type, Body);
558 
559     // This relocation does not require got entry, but it is relative to got and
560     // needs it to be created. Here we request for that.
561     if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC)
562       HasGotOffRel = true;
563 
564     uintX_t Addend = getAddend<ELFT>(RI);
565     const uint8_t *BufLoc = Buf + RI.r_offset;
566     if (!RelTy::IsRela)
567       Addend += Target->getImplicitAddend(BufLoc, Type);
568     if (Config->EMachine == EM_MIPS) {
569       Addend += findMipsPairedAddend<ELFT>(Buf, BufLoc, Body, &RI, E);
570       if (Type == R_MIPS_LO16 && Expr == R_PC)
571         // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
572         // symbol. In that case we should use the following formula for
573         // calculation "AHL + GP – P + 4". Let's add 4 right here.
574         // For details see p. 4-19 at
575         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
576         Addend += 4;
577     }
578 
579     if (unsigned Processed =
580             handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
581       I += (Processed - 1);
582       continue;
583     }
584 
585     if (Expr == R_GOT && !isRelRelative<ELFT>(Expr, Type, Body) &&
586         Config->Shared)
587       AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body,
588               getAddend<ELFT>(RI)});
589 
590     // If a symbol in a DSO is referenced directly instead of through GOT
591     // in a read-only section, we need to create a copy relocation for the
592     // symbol.
593     if (auto *B = dyn_cast<SharedSymbol<ELFT>>(&Body)) {
594       if (!IsWrite && needsCopyRel(Expr, *B)) {
595         if (!B->needsCopy())
596           addCopyRelSymbol(B);
597         C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
598         continue;
599       }
600     }
601 
602     bool Preemptible = Body.isPreemptible();
603 
604     // If a relocation needs PLT, we create a PLT and a GOT slot
605     // for the symbol.
606     PltNeed NeedPlt = needsPlt(Expr, Type, Body);
607     if (NeedPlt) {
608       if (NeedPlt == Plt_Implicit)
609         Body.NeedsCopyOrPltAddr = true;
610       RelExpr E = Expr;
611       if (Expr == R_PPC_OPD)
612         E = R_PPC_PLT_OPD;
613       else if (Expr == R_PC)
614         E = R_PLT_PC;
615       else if (Expr == R_ABS)
616         E = R_PLT;
617       C.Relocations.push_back({E, Type, Offset, Addend, &Body});
618 
619       if (Body.isInPlt())
620         continue;
621       Out<ELFT>::Plt->addEntry(Body);
622 
623       uint32_t Rel;
624       if (Body.isGnuIFunc())
625         Rel = Preemptible ? Target->PltRel : Target->IRelativeRel;
626       else
627         Rel = Target->UseLazyBinding ? Target->PltRel : Target->GotRel;
628 
629       if (Target->UseLazyBinding) {
630         Out<ELFT>::GotPlt->addEntry(Body);
631         Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
632                                       Body.getGotPltOffset<ELFT>(),
633                                       !Preemptible, &Body, 0});
634       } else {
635         if (Body.isInGot())
636           continue;
637         Out<ELFT>::Got->addEntry(Body);
638         AddDyn({Rel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), !Preemptible,
639                 &Body, 0});
640       }
641       continue;
642     }
643 
644     // We decided not to use a plt. Optimize a reference to the plt to a
645     // reference to the symbol itself.
646     if (Expr == R_PLT_PC)
647       Expr = R_PC;
648     if (Expr == R_PPC_PLT_OPD)
649       Expr = R_PPC_OPD;
650     if (Expr == R_PLT)
651       Expr = R_ABS;
652 
653     if (Target->needsThunk(Type, File, Body)) {
654       C.Relocations.push_back({R_THUNK, Type, Offset, Addend, &Body});
655       continue;
656     }
657 
658     // If a relocation needs GOT, we create a GOT slot for the symbol.
659     if (refersToGotEntry(Expr)) {
660       uint32_t T = Body.isTls() ? Target->getTlsGotRel(Type) : Type;
661       if (Config->EMachine == EM_MIPS && Expr == R_GOT_OFF)
662         Addend -= MipsGPOffset;
663       C.Relocations.push_back({Expr, T, Offset, Addend, &Body});
664       if (Body.isInGot())
665         continue;
666       Out<ELFT>::Got->addEntry(Body);
667 
668       if (Config->EMachine == EM_MIPS)
669         // MIPS ABI has special rules to process GOT entries
670         // and doesn't require relocation entries for them.
671         // See "Global Offset Table" in Chapter 5 in the following document
672         // for detailed description:
673         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
674         continue;
675 
676       if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) {
677         uint32_t DynType;
678         if (Body.isTls())
679           DynType = Target->TlsGotRel;
680         else if (Preemptible)
681           DynType = Target->GotRel;
682         else
683           DynType = Target->RelativeRel;
684         AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
685                 !Preemptible, &Body, 0});
686       }
687       continue;
688     }
689 
690     if (Preemptible) {
691       // We don't know anything about the finaly symbol. Just ask the dynamic
692       // linker to handle the relocation for us.
693       AddDyn({Target->getDynRel(Type), C.OutSec, Offset, false, &Body, Addend});
694       // MIPS ABI turns using of GOT and dynamic relocations inside out.
695       // While regular ABI uses dynamic relocations to fill up GOT entries
696       // MIPS ABI requires dynamic linker to fills up GOT entries using
697       // specially sorted dynamic symbol table. This affects even dynamic
698       // relocations against symbols which do not require GOT entries
699       // creation explicitly, i.e. do not have any GOT-relocations. So if
700       // a preemptible symbol has a dynamic relocation we anyway have
701       // to create a GOT entry for it.
702       // If a non-preemptible symbol has a dynamic relocation against it,
703       // dynamic linker takes it st_value, adds offset and writes down
704       // result of the dynamic relocation. In case of preemptible symbol
705       // dynamic linker performs symbol resolution, writes the symbol value
706       // to the GOT entry and reads the GOT entry when it needs to perform
707       // a dynamic relocation.
708       // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
709       if (Config->EMachine == EM_MIPS && !Body.isInGot())
710         Out<ELFT>::Got->addEntry(Body);
711       continue;
712     }
713 
714     // We know that this is the final symbol. If the program being produced
715     // is position independent, the final value is still not known.
716     // If the relocation depends on the symbol value (not the size or distances
717     // in the output), we still need some help from the dynamic linker.
718     // We can however do better than just copying the incoming relocation. We
719     // can process some of it and and just ask the dynamic linker to add the
720     // load address.
721     if (!Config->Pic || isRelRelative<ELFT>(Expr, Type, Body)) {
722       if (Config->EMachine == EM_MIPS && Body.isLocal() &&
723           (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32))
724         Addend += File.getMipsGp0();
725       C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
726       continue;
727     }
728 
729     if (Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
730       Addend += getPPC64TocBase();
731     AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, Addend});
732     C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
733   }
734 
735   // Scan relocations for necessary thunks.
736   if (Config->EMachine == EM_MIPS)
737     scanRelocsForThunks(File, Rels);
738 }
739 
740 template <class ELFT> void Writer<ELFT>::scanRelocs(InputSection<ELFT> &C) {
741   // Scan all relocations. Each relocation goes through a series
742   // of tests to determine if it needs special treatment, such as
743   // creating GOT, PLT, copy relocations, etc.
744   // Note that relocations for non-alloc sections are directly
745   // processed by InputSection::relocateNative.
746   if (C.getSectionHdr()->sh_flags & SHF_ALLOC)
747     for (const Elf_Shdr *RelSec : C.RelocSections)
748       scanRelocs(C, *RelSec);
749 }
750 
751 template <class ELFT>
752 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &S,
753                               const Elf_Shdr &RelSec) {
754   ELFFile<ELFT> &EObj = S.getFile()->getObj();
755   if (RelSec.sh_type == SHT_RELA)
756     scanRelocs(S, EObj.relas(&RelSec));
757   else
758     scanRelocs(S, EObj.rels(&RelSec));
759 }
760 
761 template <class ELFT>
762 static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) {
763   if (!Config->NoUndefined) {
764     if (Config->Relocatable)
765       return;
766     if (Config->Shared)
767       if (Sym->Backref->Visibility == STV_DEFAULT)
768         return;
769   }
770 
771   std::string Msg = "undefined symbol: " + Sym->getName().str();
772   if (InputFile *File = Symtab.findFile(Sym))
773     Msg += " in " + File->getName().str();
774   if (Config->NoinhibitExec)
775     warning(Msg);
776   else
777     error(Msg);
778 }
779 
780 template <class ELFT>
781 static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
782                                const SymbolBody &B) {
783   if (B.isFile())
784     return false;
785 
786   // We keep sections in symtab for relocatable output.
787   if (B.isSection())
788     return Config->Relocatable;
789 
790   // If sym references a section in a discarded group, don't keep it.
791   if (Sec == &InputSection<ELFT>::Discarded)
792     return false;
793 
794   if (Config->DiscardNone)
795     return true;
796 
797   // In ELF assembly .L symbols are normally discarded by the assembler.
798   // If the assembler fails to do so, the linker discards them if
799   // * --discard-locals is used.
800   // * The symbol is in a SHF_MERGE section, which is normally the reason for
801   //   the assembler keeping the .L symbol.
802   if (!SymName.startswith(".L") && !SymName.empty())
803     return true;
804 
805   if (Config->DiscardLocals)
806     return false;
807 
808   return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
809 }
810 
811 // Local symbols are not in the linker's symbol table. This function scans
812 // each object file's symbol table to copy local symbols to the output.
813 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
814   if (!Out<ELFT>::SymTab)
815     return;
816   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
817        Symtab.getObjectFiles()) {
818     const char *StrTab = F->getStringTable().data();
819     for (SymbolBody *B : F->getLocalSymbols()) {
820       auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
821       // No reason to keep local undefined symbol in symtab.
822       if (!DR)
823         continue;
824       StringRef SymName(StrTab + B->getNameOffset());
825       InputSectionBase<ELFT> *Sec = DR->Section;
826       if (!shouldKeepInSymtab<ELFT>(Sec, SymName, *B))
827         continue;
828       if (Sec) {
829         if (!Sec->Live)
830           continue;
831 
832         // Garbage collection is normally able to remove local symbols if they
833         // point to gced sections. In the case of SHF_MERGE sections, we want it
834         // to also be able to drop them if part of the section is gced.
835         // We could look at the section offset map to keep some of these
836         // symbols, but almost all local symbols are .L* symbols, so it
837         // is probably not worth the complexity.
838         if (Config->GcSections && isa<MergeInputSection<ELFT>>(Sec))
839           continue;
840       }
841       ++Out<ELFT>::SymTab->NumLocals;
842       if (Config->Relocatable)
843         B->DynsymIndex = Out<ELFT>::SymTab->NumLocals;
844       F->KeptLocalSyms.push_back(
845           std::make_pair(DR, Out<ELFT>::SymTab->StrTabSec.addString(SymName)));
846     }
847   }
848 }
849 
850 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
851 // we would like to make sure appear is a specific order to maximize their
852 // coverage by a single signed 16-bit offset from the TOC base pointer.
853 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
854 // sections. This will put it next to the loaded special PPC64 sections (and,
855 // thus, within reach of the TOC base pointer).
856 static int getPPC64SectionRank(StringRef SectionName) {
857   return StringSwitch<int>(SectionName)
858       .Case(".tocbss", 0)
859       .Case(".branch_lt", 2)
860       .Case(".toc", 3)
861       .Case(".toc1", 4)
862       .Case(".opd", 5)
863       .Default(1);
864 }
865 
866 template <class ELFT> static bool isRelroSection(OutputSectionBase<ELFT> *Sec) {
867   if (!Config->ZRelro)
868     return false;
869   typename ELFT::uint Flags = Sec->getFlags();
870   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
871     return false;
872   if (Flags & SHF_TLS)
873     return true;
874   uint32_t Type = Sec->getType();
875   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
876       Type == SHT_PREINIT_ARRAY)
877     return true;
878   if (Sec == Out<ELFT>::GotPlt)
879     return Config->ZNow;
880   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
881     return true;
882   StringRef S = Sec->getName();
883   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
884          S == ".eh_frame";
885 }
886 
887 // Output section ordering is determined by this function.
888 template <class ELFT>
889 static bool compareSections(OutputSectionBase<ELFT> *A,
890                             OutputSectionBase<ELFT> *B) {
891   typedef typename ELFT::uint uintX_t;
892 
893   int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName());
894   if (Comp != 0)
895     return Comp < 0;
896 
897   uintX_t AFlags = A->getFlags();
898   uintX_t BFlags = B->getFlags();
899 
900   // Allocatable sections go first to reduce the total PT_LOAD size and
901   // so debug info doesn't change addresses in actual code.
902   bool AIsAlloc = AFlags & SHF_ALLOC;
903   bool BIsAlloc = BFlags & SHF_ALLOC;
904   if (AIsAlloc != BIsAlloc)
905     return AIsAlloc;
906 
907   // We don't have any special requirements for the relative order of
908   // two non allocatable sections.
909   if (!AIsAlloc)
910     return false;
911 
912   // We want the read only sections first so that they go in the PT_LOAD
913   // covering the program headers at the start of the file.
914   bool AIsWritable = AFlags & SHF_WRITE;
915   bool BIsWritable = BFlags & SHF_WRITE;
916   if (AIsWritable != BIsWritable)
917     return BIsWritable;
918 
919   // For a corresponding reason, put non exec sections first (the program
920   // header PT_LOAD is not executable).
921   bool AIsExec = AFlags & SHF_EXECINSTR;
922   bool BIsExec = BFlags & SHF_EXECINSTR;
923   if (AIsExec != BIsExec)
924     return BIsExec;
925 
926   // If we got here we know that both A and B are in the same PT_LOAD.
927 
928   // The TLS initialization block needs to be a single contiguous block in a R/W
929   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
930   // sections are placed here as they don't take up virtual address space in the
931   // PT_LOAD.
932   bool AIsTls = AFlags & SHF_TLS;
933   bool BIsTls = BFlags & SHF_TLS;
934   if (AIsTls != BIsTls)
935     return AIsTls;
936 
937   // The next requirement we have is to put nobits sections last. The
938   // reason is that the only thing the dynamic linker will see about
939   // them is a p_memsz that is larger than p_filesz. Seeing that it
940   // zeros the end of the PT_LOAD, so that has to correspond to the
941   // nobits sections.
942   bool AIsNoBits = A->getType() == SHT_NOBITS;
943   bool BIsNoBits = B->getType() == SHT_NOBITS;
944   if (AIsNoBits != BIsNoBits)
945     return BIsNoBits;
946 
947   // We place RelRo section before plain r/w ones.
948   bool AIsRelRo = isRelroSection(A);
949   bool BIsRelRo = isRelroSection(B);
950   if (AIsRelRo != BIsRelRo)
951     return AIsRelRo;
952 
953   // Some architectures have additional ordering restrictions for sections
954   // within the same PT_LOAD.
955   if (Config->EMachine == EM_PPC64)
956     return getPPC64SectionRank(A->getName()) <
957            getPPC64SectionRank(B->getName());
958 
959   return false;
960 }
961 
962 // The .bss section does not exist if no input file has a .bss section.
963 // This function creates one if that's the case.
964 template <class ELFT> void Writer<ELFT>::ensureBss() {
965   if (Out<ELFT>::Bss)
966     return;
967   Out<ELFT>::Bss =
968       new OutputSection<ELFT>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
969   OwningSections.emplace_back(Out<ELFT>::Bss);
970   OutputSections.push_back(Out<ELFT>::Bss);
971 }
972 
973 // Until this function is called, common symbols do not belong to any section.
974 // This function adds them to end of BSS section.
975 template <class ELFT>
976 void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
977   if (Syms.empty())
978     return;
979 
980   // Sort the common symbols by alignment as an heuristic to pack them better.
981   std::stable_sort(Syms.begin(), Syms.end(),
982                    [](const DefinedCommon *A, const DefinedCommon *B) {
983                      return A->Alignment > B->Alignment;
984                    });
985 
986   ensureBss();
987   uintX_t Off = Out<ELFT>::Bss->getSize();
988   for (DefinedCommon *C : Syms) {
989     Off = alignTo(Off, C->Alignment);
990     Out<ELFT>::Bss->updateAlign(C->Alignment);
991     C->OffsetInBss = Off;
992     Off += C->Size;
993   }
994 
995   Out<ELFT>::Bss->setSize(Off);
996 }
997 
998 template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
999   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1000 
1001   uintX_t SecAlign = SS->File->getSection(SS->Sym)->sh_addralign;
1002   uintX_t SymValue = SS->Sym.st_value;
1003   int TrailingZeros =
1004       std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
1005   return 1 << TrailingZeros;
1006 }
1007 
1008 // Reserve space in .bss for copy relocation.
1009 template <class ELFT>
1010 void Writer<ELFT>::addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
1011   ensureBss();
1012   uintX_t Align = getAlignment(SS);
1013   uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Align);
1014   Out<ELFT>::Bss->setSize(Off + SS->template getSize<ELFT>());
1015   Out<ELFT>::Bss->updateAlign(Align);
1016   uintX_t Shndx = SS->Sym.st_shndx;
1017   uintX_t Value = SS->Sym.st_value;
1018   // Look through the DSO's dynamic symbol for aliases and create a dynamic
1019   // symbol for each one. This causes the copy relocation to correctly interpose
1020   // any aliases.
1021   for (SharedSymbol<ELFT> &S : SS->File->getSharedSymbols()) {
1022     if (S.Sym.st_shndx != Shndx || S.Sym.st_value != Value)
1023       continue;
1024     S.OffsetInBss = Off;
1025     S.NeedsCopyOrPltAddr = true;
1026     S.Backref->IsUsedInRegularObj = true;
1027   }
1028   Out<ELFT>::RelaDyn->addReloc(
1029       {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
1030 }
1031 
1032 template <class ELFT>
1033 StringRef Writer<ELFT>::getOutputSectionName(InputSectionBase<ELFT> *S) const {
1034   StringRef Dest = Script<ELFT>::X->getOutputSection(S);
1035   if (!Dest.empty())
1036     return Dest;
1037 
1038   StringRef Name = S->getSectionName();
1039   for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
1040                       ".init_array.", ".fini_array.", ".ctors.", ".dtors.",
1041                       ".tbss.", ".gcc_except_table.", ".tdata."})
1042     if (Name.startswith(V))
1043       return V.drop_back();
1044   return Name;
1045 }
1046 
1047 template <class ELFT>
1048 void reportDiscarded(InputSectionBase<ELFT> *IS,
1049                      const std::unique_ptr<elf::ObjectFile<ELFT>> &File) {
1050   if (!Config->PrintGcSections || !IS || IS->Live)
1051     return;
1052   llvm::errs() << "removing unused section from '" << IS->getSectionName()
1053                << "' in file '" << File->getName() << "'\n";
1054 }
1055 
1056 template <class ELFT>
1057 bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const {
1058   return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
1059          Script<ELFT>::X->isDiscarded(S);
1060 }
1061 
1062 template <class ELFT>
1063 static SymbolBody *
1064 addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name,
1065                      OutputSectionBase<ELFT> &Sec, typename ELFT::uint Val) {
1066   if (!Table.find(Name))
1067     return nullptr;
1068   return Table.addSynthetic(Name, Sec, Val);
1069 }
1070 
1071 // The beginning and the ending of .rel[a].plt section are marked
1072 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
1073 // executable. The runtime needs these symbols in order to resolve
1074 // all IRELATIVE relocs on startup. For dynamic executables, we don't
1075 // need these symbols, since IRELATIVE relocs are resolved through GOT
1076 // and PLT. For details, see http://www.airs.com/blog/archives/403.
1077 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1078   if (isOutputDynamic() || !Out<ELFT>::RelaPlt)
1079     return;
1080   StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
1081   ElfSym<ELFT>::RelaIpltStart =
1082       addOptionalSynthetic(Symtab, S, *Out<ELFT>::RelaPlt, 0);
1083 
1084   S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
1085   ElfSym<ELFT>::RelaIpltEnd = addOptionalSynthetic(
1086       Symtab, S, *Out<ELFT>::RelaPlt, DefinedSynthetic<ELFT>::SectionEnd);
1087 }
1088 
1089 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
1090   if (!B.Backref->IsUsedInRegularObj)
1091     return false;
1092 
1093   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
1094     // Exclude symbols pointing to garbage-collected sections.
1095     if (D->Section && !D->Section->Live)
1096       return false;
1097   }
1098   return true;
1099 }
1100 
1101 // This class knows how to create an output section for a given
1102 // input section. Output section type is determined by various
1103 // factors, including input section's sh_flags, sh_type and
1104 // linker scripts.
1105 namespace {
1106 template <class ELFT> class OutputSectionFactory {
1107   typedef typename ELFT::Shdr Elf_Shdr;
1108   typedef typename ELFT::uint uintX_t;
1109 
1110 public:
1111   std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
1112                                                     StringRef OutsecName);
1113 
1114   OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type,
1115                                   uintX_t Flags) {
1116     return Map.lookup({Name, Type, Flags, 0});
1117   }
1118 
1119 private:
1120   SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1121                                        StringRef OutsecName);
1122 
1123   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map;
1124 };
1125 }
1126 
1127 template <class ELFT>
1128 std::pair<OutputSectionBase<ELFT> *, bool>
1129 OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1130                                    StringRef OutsecName) {
1131   SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
1132   OutputSectionBase<ELFT> *&Sec = Map[Key];
1133   if (Sec)
1134     return {Sec, false};
1135 
1136   switch (C->SectionKind) {
1137   case InputSectionBase<ELFT>::Regular:
1138     Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
1139     break;
1140   case InputSectionBase<ELFT>::EHFrame:
1141     Sec = new EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
1142     break;
1143   case InputSectionBase<ELFT>::Merge:
1144     Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags,
1145                                        Key.Alignment);
1146     break;
1147   case InputSectionBase<ELFT>::MipsReginfo:
1148     Sec = new MipsReginfoOutputSection<ELFT>();
1149     break;
1150   }
1151   return {Sec, true};
1152 }
1153 
1154 template <class ELFT>
1155 SectionKey<ELFT::Is64Bits>
1156 OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C,
1157                                       StringRef OutsecName) {
1158   const Elf_Shdr *H = C->getSectionHdr();
1159   uintX_t Flags = H->sh_flags & ~SHF_GROUP;
1160 
1161   // For SHF_MERGE we create different output sections for each alignment.
1162   // This makes each output section simple and keeps a single level mapping from
1163   // input to output.
1164   uintX_t Alignment = 0;
1165   if (isa<MergeInputSection<ELFT>>(C))
1166     Alignment = std::max(H->sh_addralign, H->sh_entsize);
1167 
1168   // GNU as can give .eh_frame section type SHT_PROGBITS or SHT_X86_64_UNWIND
1169   // depending on the construct. We want to canonicalize it so that
1170   // there is only one .eh_frame in the end.
1171   uint32_t Type = H->sh_type;
1172   if (Type == SHT_PROGBITS && Config->EMachine == EM_X86_64 &&
1173       isa<EHInputSection<ELFT>>(C))
1174     Type = SHT_X86_64_UNWIND;
1175 
1176   return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment};
1177 }
1178 
1179 // The linker is expected to define some symbols depending on
1180 // the linking result. This function defines such symbols.
1181 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
1182   if (Config->EMachine == EM_MIPS) {
1183     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
1184     // so that it points to an absolute address which is relative to GOT.
1185     // See "Global Data Symbols" in Chapter 6 in the following document:
1186     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1187     ElfSym<ELFT>::MipsGp =
1188         Symtab.addSynthetic("_gp", *Out<ELFT>::Got, MipsGPOffset);
1189 
1190     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
1191     // start of function and 'gp' pointer into GOT.
1192     ElfSym<ELFT>::MipsGpDisp =
1193         addOptionalSynthetic(Symtab, "_gp_disp", *Out<ELFT>::Got, MipsGPOffset);
1194     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
1195     // pointer. This symbol is used in the code generated by .cpload pseudo-op
1196     // in case of using -mno-shared option.
1197     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
1198     ElfSym<ELFT>::MipsLocalGp = addOptionalSynthetic(
1199         Symtab, "__gnu_local_gp", *Out<ELFT>::Got, MipsGPOffset);
1200   }
1201 
1202   // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
1203   // is magical and is used to produce a R_386_GOTPC relocation.
1204   // The R_386_GOTPC relocation value doesn't actually depend on the
1205   // symbol value, so it could use an index of STN_UNDEF which, according
1206   // to the spec, means the symbol value is 0.
1207   // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
1208   // the object file.
1209   // The situation is even stranger on x86_64 where the assembly doesn't
1210   // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
1211   // an undefined symbol in the .o files.
1212   // Given that the symbol is effectively unused, we just create a dummy
1213   // hidden one to avoid the undefined symbol error.
1214   if (!Config->Relocatable)
1215     Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
1216 
1217   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
1218   // static linking the linker is required to optimize away any references to
1219   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
1220   // to avoid the undefined symbol error.
1221   if (!isOutputDynamic())
1222     Symtab.addIgnored("__tls_get_addr");
1223 
1224   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
1225                        DefinedRegular<ELFT> *&Sym2) {
1226     Sym1 = Symtab.addIgnored(S, STV_DEFAULT);
1227 
1228     // The name without the underscore is not a reserved name,
1229     // so it is defined only when there is a reference against it.
1230     assert(S.startswith("_"));
1231     S = S.substr(1);
1232     if (SymbolBody *B = Symtab.find(S))
1233       if (B->isUndefined())
1234         Sym2 = Symtab.addAbsolute(S, STV_DEFAULT);
1235   };
1236 
1237   Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
1238   Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2);
1239   Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2);
1240 }
1241 
1242 // Sort input sections by section name suffixes for
1243 // __attribute__((init_priority(N))).
1244 template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) {
1245   if (S)
1246     reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
1247 }
1248 
1249 // Sort input sections by the special rule for .ctors and .dtors.
1250 template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
1251   if (S)
1252     reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
1253 }
1254 
1255 // Create output section objects and add them to OutputSections.
1256 template <class ELFT> void Writer<ELFT>::createSections() {
1257   // Add .interp first because some loaders want to see that section
1258   // on the first page of the executable file when loaded into memory.
1259   if (needsInterpSection())
1260     OutputSections.push_back(Out<ELFT>::Interp);
1261 
1262   // A core file does not usually contain unmodified segments except
1263   // the first page of the executable. Add the build ID section now
1264   // so that the section is included in the first page.
1265   if (Out<ELFT>::BuildId)
1266     OutputSections.push_back(Out<ELFT>::BuildId);
1267 
1268   // Create output sections for input object file sections.
1269   std::vector<OutputSectionBase<ELFT> *> RegularSections;
1270   OutputSectionFactory<ELFT> Factory;
1271   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
1272        Symtab.getObjectFiles()) {
1273     for (InputSectionBase<ELFT> *C : F->getSections()) {
1274       if (isDiscarded(C)) {
1275         reportDiscarded(C, F);
1276         continue;
1277       }
1278       OutputSectionBase<ELFT> *Sec;
1279       bool IsNew;
1280       std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C));
1281       if (IsNew) {
1282         OwningSections.emplace_back(Sec);
1283         OutputSections.push_back(Sec);
1284         RegularSections.push_back(Sec);
1285       }
1286       Sec->addSection(C);
1287     }
1288   }
1289 
1290   Out<ELFT>::Bss = static_cast<OutputSection<ELFT> *>(
1291       Factory.lookup(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE));
1292 
1293   // If we have a .opd section (used under PPC64 for function descriptors),
1294   // store a pointer to it here so that we can use it later when processing
1295   // relocations.
1296   Out<ELFT>::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC);
1297 
1298   Out<ELFT>::Dynamic->PreInitArraySec = Factory.lookup(
1299       ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC);
1300   Out<ELFT>::Dynamic->InitArraySec =
1301       Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC);
1302   Out<ELFT>::Dynamic->FiniArraySec =
1303       Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC);
1304 
1305   // Sort section contents for __attribute__((init_priority(N)).
1306   sortInitFini(Out<ELFT>::Dynamic->InitArraySec);
1307   sortInitFini(Out<ELFT>::Dynamic->FiniArraySec);
1308   sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1309   sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1310 
1311   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1312   // symbols for sections, so that the runtime can get the start and end
1313   // addresses of each section by section name. Add such symbols.
1314   if (!Config->Relocatable) {
1315     addStartEndSymbols();
1316     for (OutputSectionBase<ELFT> *Sec : RegularSections)
1317       addStartStopSymbols(Sec);
1318   }
1319 
1320   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1321   // It should be okay as no one seems to care about the type.
1322   // Even the author of gold doesn't remember why gold behaves that way.
1323   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1324   if (isOutputDynamic())
1325     Symtab.addSynthetic("_DYNAMIC", *Out<ELFT>::Dynamic, 0);
1326 
1327   // Define __rel[a]_iplt_{start,end} symbols if needed.
1328   addRelIpltSymbols();
1329 
1330   if (Out<ELFT>::EhFrameHdr->Sec)
1331     Out<ELFT>::EhFrameHdr->Sec->finalize();
1332 
1333   // Scan relocations. This must be done after every symbol is declared so that
1334   // we can correctly decide if a dynamic relocation is needed.
1335   // Check size() each time to guard against .bss being created.
1336   for (unsigned I = 0; I < OutputSections.size(); ++I) {
1337     OutputSectionBase<ELFT> *Sec = OutputSections[I];
1338     Sec->forEachInputSection([&](InputSectionBase<ELFT> *S) {
1339       if (auto *IS = dyn_cast<InputSection<ELFT>>(S)) {
1340         // Set OutSecOff so that scanRelocs can use it.
1341         uintX_t Off = alignTo(Sec->getSize(), S->Align);
1342         IS->OutSecOff = Off;
1343 
1344         scanRelocs(*IS);
1345 
1346         // Now that scan relocs possibly changed the size, update the offset.
1347         Sec->setSize(Off + S->getSize());
1348       } else if (auto *EH = dyn_cast<EHInputSection<ELFT>>(S)) {
1349         if (EH->RelocSection)
1350           scanRelocs(*EH, *EH->RelocSection);
1351       }
1352     });
1353   }
1354 
1355   // Now that we have defined all possible symbols including linker-
1356   // synthesized ones. Visit all symbols to give the finishing touches.
1357   std::vector<DefinedCommon *> CommonSymbols;
1358   for (Symbol *S : Symtab.getSymbols()) {
1359     SymbolBody *Body = S->Body;
1360 
1361     // Set "used" bit for --as-needed.
1362     if (S->IsUsedInRegularObj && !S->isWeak())
1363       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
1364         SS->File->IsUsed = true;
1365 
1366     if (Body->isUndefined() && !S->isWeak())
1367       reportUndefined<ELFT>(Symtab, Body);
1368 
1369     if (auto *C = dyn_cast<DefinedCommon>(Body))
1370       CommonSymbols.push_back(C);
1371 
1372     if (!includeInSymtab<ELFT>(*Body))
1373       continue;
1374     if (Out<ELFT>::SymTab)
1375       Out<ELFT>::SymTab->addSymbol(Body);
1376 
1377     if (isOutputDynamic() && S->includeInDynsym()) {
1378       Out<ELFT>::DynSymTab->addSymbol(Body);
1379       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
1380         Out<ELFT>::VerNeed->addSymbol(SS);
1381     }
1382   }
1383 
1384   // Do not proceed if there was an undefined symbol.
1385   if (HasError)
1386     return;
1387 
1388   addCommonSymbols(CommonSymbols);
1389 
1390   // So far we have added sections from input object files.
1391   // This function adds linker-created Out<ELFT>::* sections.
1392   addPredefinedSections();
1393 
1394   std::stable_sort(OutputSections.begin(), OutputSections.end(),
1395                    compareSections<ELFT>);
1396 
1397   unsigned I = 1;
1398   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1399     Sec->SectionIndex = I++;
1400     Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName()));
1401   }
1402 
1403   // Finalizers fix each section's size.
1404   // .dynsym is finalized early since that may fill up .gnu.hash.
1405   if (isOutputDynamic())
1406     Out<ELFT>::DynSymTab->finalize();
1407 
1408   // Fill other section headers. The dynamic table is finalized
1409   // at the end because some tags like RELSZ depend on result
1410   // of finalizing other sections. The dynamic string table is
1411   // finalized once the .dynamic finalizer has added a few last
1412   // strings. See DynamicSection::finalize()
1413   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1414     if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic)
1415       Sec->finalize();
1416 
1417   if (isOutputDynamic())
1418     Out<ELFT>::Dynamic->finalize();
1419 }
1420 
1421 template <class ELFT> bool Writer<ELFT>::needsGot() {
1422   if (!Out<ELFT>::Got->empty())
1423     return true;
1424 
1425   // We add the .got section to the result for dynamic MIPS target because
1426   // its address and properties are mentioned in the .dynamic section.
1427   if (Config->EMachine == EM_MIPS)
1428     return true;
1429 
1430   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
1431   // we need to emit a GOT even if it's empty.
1432   return HasGotOffRel;
1433 }
1434 
1435 // This function add Out<ELFT>::* sections to OutputSections.
1436 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1437   auto Add = [&](OutputSectionBase<ELFT> *C) {
1438     if (C)
1439       OutputSections.push_back(C);
1440   };
1441 
1442   // This order is not the same as the final output order
1443   // because we sort the sections using their attributes below.
1444   Add(Out<ELFT>::SymTab);
1445   Add(Out<ELFT>::ShStrTab);
1446   Add(Out<ELFT>::StrTab);
1447   if (isOutputDynamic()) {
1448     Add(Out<ELFT>::DynSymTab);
1449     if (Out<ELFT>::VerNeed->getNeedNum() != 0) {
1450       Add(Out<ELFT>::VerSym);
1451       Add(Out<ELFT>::VerNeed);
1452     }
1453     Add(Out<ELFT>::GnuHashTab);
1454     Add(Out<ELFT>::HashTab);
1455     Add(Out<ELFT>::Dynamic);
1456     Add(Out<ELFT>::DynStrTab);
1457     if (Out<ELFT>::RelaDyn->hasRelocs())
1458       Add(Out<ELFT>::RelaDyn);
1459     Add(Out<ELFT>::MipsRldMap);
1460   }
1461 
1462   // We always need to add rel[a].plt to output if it has entries.
1463   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
1464   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
1465     Add(Out<ELFT>::RelaPlt);
1466     Out<ELFT>::RelaPlt->Static = !isOutputDynamic();
1467   }
1468 
1469   if (needsGot())
1470     Add(Out<ELFT>::Got);
1471   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
1472     Add(Out<ELFT>::GotPlt);
1473   if (!Out<ELFT>::Plt->empty())
1474     Add(Out<ELFT>::Plt);
1475   if (Out<ELFT>::EhFrameHdr->Live)
1476     Add(Out<ELFT>::EhFrameHdr);
1477 }
1478 
1479 // The linker is expected to define SECNAME_start and SECNAME_end
1480 // symbols for a few sections. This function defines them.
1481 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1482   auto Define = [&](StringRef Start, StringRef End,
1483                     OutputSectionBase<ELFT> *OS) {
1484     if (OS) {
1485       Symtab.addSynthetic(Start, *OS, 0);
1486       Symtab.addSynthetic(End, *OS, DefinedSynthetic<ELFT>::SectionEnd);
1487     } else {
1488       Symtab.addIgnored(Start);
1489       Symtab.addIgnored(End);
1490     }
1491   };
1492 
1493   Define("__preinit_array_start", "__preinit_array_end",
1494          Out<ELFT>::Dynamic->PreInitArraySec);
1495   Define("__init_array_start", "__init_array_end",
1496          Out<ELFT>::Dynamic->InitArraySec);
1497   Define("__fini_array_start", "__fini_array_end",
1498          Out<ELFT>::Dynamic->FiniArraySec);
1499 }
1500 
1501 // If a section name is valid as a C identifier (which is rare because of
1502 // the leading '.'), linkers are expected to define __start_<secname> and
1503 // __stop_<secname> symbols. They are at beginning and end of the section,
1504 // respectively. This is not requested by the ELF standard, but GNU ld and
1505 // gold provide the feature, and used by many programs.
1506 template <class ELFT>
1507 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
1508   StringRef S = Sec->getName();
1509   if (!isValidCIdentifier(S))
1510     return;
1511   StringSaver Saver(Alloc);
1512   StringRef Start = Saver.save("__start_" + S);
1513   StringRef Stop = Saver.save("__stop_" + S);
1514   if (SymbolBody *B = Symtab.find(Start))
1515     if (B->isUndefined())
1516       Symtab.addSynthetic(Start, *Sec, 0);
1517   if (SymbolBody *B = Symtab.find(Stop))
1518     if (B->isUndefined())
1519       Symtab.addSynthetic(Stop, *Sec, DefinedSynthetic<ELFT>::SectionEnd);
1520 }
1521 
1522 template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) {
1523   if (!(Sec->getFlags() & SHF_ALLOC))
1524     return false;
1525 
1526   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1527   // responsible for allocating space for them, not the PT_LOAD that
1528   // contains the TLS initialization image.
1529   if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS)
1530     return false;
1531   return true;
1532 }
1533 
1534 static uint32_t toPhdrFlags(uint64_t Flags) {
1535   uint32_t Ret = PF_R;
1536   if (Flags & SHF_WRITE)
1537     Ret |= PF_W;
1538   if (Flags & SHF_EXECINSTR)
1539     Ret |= PF_X;
1540   return Ret;
1541 }
1542 
1543 // Decide which program headers to create and which sections to include in each
1544 // one.
1545 template <class ELFT> void Writer<ELFT>::createPhdrs() {
1546   auto AddHdr = [this](unsigned Type, unsigned Flags) {
1547     return &*Phdrs.emplace(Phdrs.end(), Type, Flags);
1548   };
1549 
1550   auto AddSec = [](Phdr &Hdr, OutputSectionBase<ELFT> *Sec) {
1551     Hdr.Last = Sec;
1552     if (!Hdr.First)
1553       Hdr.First = Sec;
1554     Hdr.H.p_align = std::max<uintX_t>(Hdr.H.p_align, Sec->getAlign());
1555   };
1556 
1557   // The first phdr entry is PT_PHDR which describes the program header itself.
1558   Phdr &Hdr = *AddHdr(PT_PHDR, PF_R);
1559   AddSec(Hdr, Out<ELFT>::ProgramHeaders);
1560 
1561   // PT_INTERP must be the second entry if exists.
1562   if (needsInterpSection()) {
1563     Phdr &Hdr = *AddHdr(PT_INTERP, toPhdrFlags(Out<ELFT>::Interp->getFlags()));
1564     AddSec(Hdr, Out<ELFT>::Interp);
1565   }
1566 
1567   // Add the first PT_LOAD segment for regular output sections.
1568   uintX_t Flags = PF_R;
1569   Phdr *Load = AddHdr(PT_LOAD, Flags);
1570   AddSec(*Load, Out<ELFT>::ElfHeader);
1571   AddSec(*Load, Out<ELFT>::ProgramHeaders);
1572 
1573   Phdr TlsHdr(PT_TLS, PF_R);
1574   Phdr RelRo(PT_GNU_RELRO, PF_R);
1575   Phdr Note(PT_NOTE, PF_R);
1576   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1577     if (!(Sec->getFlags() & SHF_ALLOC))
1578       break;
1579 
1580     // If we meet TLS section then we create TLS header
1581     // and put all TLS sections inside for futher use when
1582     // assign addresses.
1583     if (Sec->getFlags() & SHF_TLS)
1584       AddSec(TlsHdr, Sec);
1585 
1586     if (!needsPtLoad<ELFT>(Sec))
1587       continue;
1588 
1589     // If flags changed then we want new load segment.
1590     uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
1591     if (Flags != NewFlags) {
1592       Load = AddHdr(PT_LOAD, NewFlags);
1593       Flags = NewFlags;
1594     }
1595 
1596     AddSec(*Load, Sec);
1597 
1598     if (isRelroSection(Sec))
1599       AddSec(RelRo, Sec);
1600     if (Sec->getType() == SHT_NOTE)
1601       AddSec(Note, Sec);
1602   }
1603 
1604   // Add the TLS segment unless it's empty.
1605   if (TlsHdr.First)
1606     Phdrs.push_back(std::move(TlsHdr));
1607 
1608   // Add an entry for .dynamic.
1609   if (isOutputDynamic()) {
1610     Phdr &H = *AddHdr(PT_DYNAMIC, toPhdrFlags(Out<ELFT>::Dynamic->getFlags()));
1611     AddSec(H, Out<ELFT>::Dynamic);
1612   }
1613 
1614   // PT_GNU_RELRO includes all sections that should be marked as
1615   // read-only by dynamic linker after proccessing relocations.
1616   if (RelRo.First)
1617     Phdrs.push_back(std::move(RelRo));
1618 
1619   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1620   if (Out<ELFT>::EhFrameHdr->Live) {
1621     Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME,
1622                         toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()));
1623     AddSec(Hdr, Out<ELFT>::EhFrameHdr);
1624   }
1625 
1626   // PT_GNU_STACK is a special section to tell the loader to make the
1627   // pages for the stack non-executable.
1628   if (!Config->ZExecStack)
1629     AddHdr(PT_GNU_STACK, PF_R | PF_W);
1630 
1631   if (Note.First)
1632     Phdrs.push_back(std::move(Note));
1633 
1634   Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size());
1635 }
1636 
1637 // The first section of each PT_LOAD and the first section after PT_GNU_RELRO
1638 // have to be page aligned so that the dynamic linker can set the permissions.
1639 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1640   for (const Phdr &P : Phdrs)
1641     if (P.H.p_type == PT_LOAD)
1642       P.First->PageAlign = true;
1643 
1644   for (const Phdr &P : Phdrs) {
1645     if (P.H.p_type != PT_GNU_RELRO)
1646       continue;
1647     // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1648     // have to align it to a page.
1649     auto End = OutputSections.end();
1650     auto I = std::find(OutputSections.begin(), End, P.Last);
1651     if (I == End || (I + 1) == End)
1652       continue;
1653     OutputSectionBase<ELFT> *Sec = *(I + 1);
1654     if (needsPtLoad(Sec))
1655       Sec->PageAlign = true;
1656   }
1657 }
1658 
1659 // We should set file offsets and VAs for elf header and program headers
1660 // sections. These are special, we do not include them into output sections
1661 // list, but have them to simplify the code.
1662 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1663   uintX_t BaseVA = ScriptConfig->DoLayout ? 0 : Target->getVAStart();
1664   Out<ELFT>::ElfHeader->setVA(BaseVA);
1665   Out<ELFT>::ElfHeader->setFileOffset(0);
1666   uintX_t Off = Out<ELFT>::ElfHeader->getSize();
1667   Out<ELFT>::ProgramHeaders->setVA(Off + BaseVA);
1668   Out<ELFT>::ProgramHeaders->setFileOffset(Off);
1669 }
1670 
1671 // Assign VAs (addresses at run-time) to output sections.
1672 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1673   uintX_t VA = Target->getVAStart() + Out<ELFT>::ElfHeader->getSize() +
1674                Out<ELFT>::ProgramHeaders->getSize();
1675 
1676   uintX_t ThreadBssOffset = 0;
1677   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1678     uintX_t Align = Sec->getAlign();
1679     if (Sec->PageAlign)
1680       Align = std::max<uintX_t>(Align, Target->PageSize);
1681 
1682     // We only assign VAs to allocated sections.
1683     if (needsPtLoad<ELFT>(Sec)) {
1684       VA = alignTo(VA, Align);
1685       Sec->setVA(VA);
1686       VA += Sec->getSize();
1687     } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) {
1688       uintX_t TVA = VA + ThreadBssOffset;
1689       TVA = alignTo(TVA, Align);
1690       Sec->setVA(TVA);
1691       ThreadBssOffset = TVA - VA + Sec->getSize();
1692     }
1693   }
1694 }
1695 
1696 // Adjusts the file alignment for a given output section and returns
1697 // its new file offset. The file offset must be the same with its
1698 // virtual address (modulo the page size) so that the loader can load
1699 // executables without any address adjustment.
1700 template <class ELFT, class uintX_t>
1701 static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) {
1702   uintX_t Align = Sec->getAlign();
1703   if (Sec->PageAlign)
1704     Align = std::max<uintX_t>(Align, Target->PageSize);
1705   Off = alignTo(Off, Align);
1706 
1707   // Relocatable output does not have program headers
1708   // and does not need any other offset adjusting.
1709   if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC))
1710     return Off;
1711   return alignTo(Off, Target->PageSize, Sec->getVA());
1712 }
1713 
1714 // Assign file offsets to output sections.
1715 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1716   uintX_t Off =
1717       Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
1718 
1719   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1720     if (Sec->getType() == SHT_NOBITS) {
1721       Sec->setFileOffset(Off);
1722       continue;
1723     }
1724 
1725     Off = getFileAlignment<ELFT>(Off, Sec);
1726     Sec->setFileOffset(Off);
1727     Off += Sec->getSize();
1728   }
1729   SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
1730   FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1731 }
1732 
1733 // Finalize the program headers. We call this function after we assign
1734 // file offsets and VAs to all sections.
1735 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1736   for (Phdr &P : Phdrs) {
1737     Elf_Phdr &H = P.H;
1738     OutputSectionBase<ELFT> *First = P.First;
1739     OutputSectionBase<ELFT> *Last = P.Last;
1740     if (First) {
1741       H.p_filesz = Last->getFileOff() - First->getFileOff();
1742       if (Last->getType() != SHT_NOBITS)
1743         H.p_filesz += Last->getSize();
1744       H.p_memsz = Last->getVA() + Last->getSize() - First->getVA();
1745       H.p_offset = First->getFileOff();
1746       H.p_vaddr = First->getVA();
1747     }
1748     if (H.p_type == PT_LOAD)
1749       H.p_align = Target->PageSize;
1750     else if (H.p_type == PT_GNU_RELRO)
1751       H.p_align = 1;
1752     H.p_paddr = H.p_vaddr;
1753 
1754     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1755     // so round up the size to make sure the offsets are correct.
1756     if (H.p_type == PT_TLS) {
1757       Out<ELFT>::TlsPhdr = &H;
1758       H.p_memsz = alignTo(H.p_memsz, H.p_align);
1759     }
1760   }
1761 }
1762 
1763 static uint32_t getMipsEFlags() {
1764   // FIXME: In fact ELF flags depends on ELF flags of input object files
1765   // and selected emulation. For now just use hard coded values.
1766   uint32_t V = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2;
1767   if (Config->Shared)
1768     V |= EF_MIPS_PIC;
1769   return V;
1770 }
1771 
1772 template <class ELFT> static typename ELFT::uint getEntryAddr() {
1773   if (Symbol *S = Config->EntrySym)
1774     return S->Body->getVA<ELFT>();
1775   if (Config->EntryAddr != uint64_t(-1))
1776     return Config->EntryAddr;
1777   return 0;
1778 }
1779 
1780 template <class ELFT> static uint8_t getELFEncoding() {
1781   if (ELFT::TargetEndianness == llvm::support::little)
1782     return ELFDATA2LSB;
1783   return ELFDATA2MSB;
1784 }
1785 
1786 static uint16_t getELFType() {
1787   if (Config->Pic)
1788     return ET_DYN;
1789   if (Config->Relocatable)
1790     return ET_REL;
1791   return ET_EXEC;
1792 }
1793 
1794 // This function is called after we have assigned address and size
1795 // to each section. This function fixes some predefined absolute
1796 // symbol values that depend on section address and size.
1797 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1798   auto Set = [](DefinedRegular<ELFT> *&S1, DefinedRegular<ELFT> *&S2,
1799                 uintX_t V) {
1800     if (S1)
1801       S1->Value = V;
1802     if (S2)
1803       S2->Value = V;
1804   };
1805 
1806   // _etext is the first location after the last read-only loadable segment.
1807   // _edata is the first location after the last read-write loadable segment.
1808   // _end is the first location after the uninitialized data region.
1809   for (Phdr &P : Phdrs) {
1810     Elf_Phdr &H = P.H;
1811     if (H.p_type != PT_LOAD)
1812       continue;
1813     Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, H.p_vaddr + H.p_memsz);
1814 
1815     uintX_t Val = H.p_vaddr + H.p_filesz;
1816     if (H.p_flags & PF_W)
1817       Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val);
1818     else
1819       Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val);
1820   }
1821 }
1822 
1823 template <class ELFT> void Writer<ELFT>::writeHeader() {
1824   uint8_t *Buf = Buffer->getBufferStart();
1825   memcpy(Buf, "\177ELF", 4);
1826 
1827   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1828 
1829   // Write the ELF header.
1830   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1831   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1832   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1833   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1834   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1835   EHdr->e_type = getELFType();
1836   EHdr->e_machine = FirstObj.getEMachine();
1837   EHdr->e_version = EV_CURRENT;
1838   EHdr->e_entry = getEntryAddr<ELFT>();
1839   EHdr->e_shoff = SectionHeaderOff;
1840   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1841   EHdr->e_phnum = Phdrs.size();
1842   EHdr->e_shentsize = sizeof(Elf_Shdr);
1843   EHdr->e_shnum = OutputSections.size() + 1;
1844   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1845 
1846   if (Config->EMachine == EM_MIPS)
1847     EHdr->e_flags = getMipsEFlags();
1848 
1849   if (!Config->Relocatable) {
1850     EHdr->e_phoff = sizeof(Elf_Ehdr);
1851     EHdr->e_phentsize = sizeof(Elf_Phdr);
1852   }
1853 
1854   // Write the program header table.
1855   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1856   for (Phdr &P : Phdrs)
1857     *HBuf++ = P.H;
1858 
1859   // Write the section header table. Note that the first table entry is null.
1860   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1861   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1862     Sec->writeHeaderTo(++SHdrs);
1863 }
1864 
1865 template <class ELFT> void Writer<ELFT>::openFile() {
1866   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1867       FileOutputBuffer::create(Config->OutputFile, FileSize,
1868                                FileOutputBuffer::F_executable);
1869   if (BufferOrErr)
1870     Buffer = std::move(*BufferOrErr);
1871   else
1872     error(BufferOrErr, "failed to open " + Config->OutputFile);
1873 }
1874 
1875 // Write section contents to a mmap'ed file.
1876 template <class ELFT> void Writer<ELFT>::writeSections() {
1877   uint8_t *Buf = Buffer->getBufferStart();
1878 
1879   // PPC64 needs to process relocations in the .opd section before processing
1880   // relocations in code-containing sections.
1881   if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) {
1882     Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
1883     Sec->writeTo(Buf + Sec->getFileOff());
1884   }
1885 
1886   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1887     if (Sec != Out<ELFT>::Opd)
1888       Sec->writeTo(Buf + Sec->getFileOff());
1889 }
1890 
1891 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1892   BuildIdSection<ELFT> *S = Out<ELFT>::BuildId;
1893   if (!S)
1894     return;
1895 
1896   // Compute a hash of all sections except .debug_* sections.
1897   // We skip debug sections because they tend to be very large
1898   // and their contents are very likely to be the same as long as
1899   // other sections are the same.
1900   uint8_t *Start = Buffer->getBufferStart();
1901   uint8_t *Last = Start;
1902   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1903     uint8_t *End = Start + Sec->getFileOff();
1904     if (!Sec->getName().startswith(".debug_"))
1905       S->update({Last, End});
1906     Last = End;
1907   }
1908   S->update({Last, Start + FileSize});
1909 
1910   // Fill the hash value field in the .note.gnu.build-id section.
1911   S->writeBuildId();
1912 }
1913 
1914 template void elf::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
1915 template void elf::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
1916 template void elf::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
1917 template void elf::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
1918