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