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