xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 7d2f5c4a)
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 "Relocations.h"
15 #include "Strings.h"
16 #include "SymbolTable.h"
17 #include "Target.h"
18 
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringSwitch.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 
29 using namespace lld;
30 using namespace lld::elf;
31 
32 namespace {
33 // The writer writes a SymbolTable result to a file.
34 template <class ELFT> class Writer {
35 public:
36   typedef typename ELFT::uint uintX_t;
37   typedef typename ELFT::Shdr Elf_Shdr;
38   typedef typename ELFT::Ehdr Elf_Ehdr;
39   typedef typename ELFT::Phdr Elf_Phdr;
40   typedef typename ELFT::Sym Elf_Sym;
41   typedef typename ELFT::SymRange Elf_Sym_Range;
42   typedef typename ELFT::Rela Elf_Rela;
43   void run();
44 
45 private:
46   typedef PhdrEntry<ELFT> Phdr;
47 
48   void copyLocalSymbols();
49   void addReservedSymbols();
50   void createSections();
51   void forEachRelSec(
52       std::function<void(InputSectionBase<ELFT> &, const typename ELFT::Shdr &)>
53           Fn);
54   void finalizeSections();
55   void addPredefinedSections();
56   bool needsGot();
57 
58   std::vector<Phdr> createPhdrs();
59   void assignAddresses();
60   void assignFileOffsets();
61   void assignFileOffsetsBinary();
62   void setPhdrs();
63   void fixHeaders();
64   void fixSectionAlignments();
65   void fixAbsoluteSymbols();
66   void openFile();
67   void writeHeader();
68   void writeSections();
69   void writeSectionsBinary();
70   void writeBuildId();
71 
72   std::unique_ptr<FileOutputBuffer> Buffer;
73 
74   BumpPtrAllocator Alloc;
75   std::vector<OutputSectionBase<ELFT> *> OutputSections;
76   OutputSectionFactory<ELFT> Factory;
77 
78   void addRelIpltSymbols();
79   void addStartEndSymbols();
80   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
81   OutputSectionBase<ELFT> *findSection(StringRef Name);
82 
83   std::vector<Phdr> Phdrs;
84 
85   uintX_t FileSize;
86   uintX_t SectionHeaderOff;
87 };
88 } // anonymous namespace
89 
90 template <class ELFT>
91 StringRef elf::getOutputSectionName(InputSectionBase<ELFT> *S) {
92   StringRef Name = S->Name;
93   for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
94                       ".init_array.", ".fini_array.", ".ctors.", ".dtors.",
95                       ".tbss.", ".gcc_except_table.", ".tdata.", ".ARM.exidx."})
96     if (Name.startswith(V))
97       return V.drop_back();
98   return Name;
99 }
100 
101 template <class ELFT> void elf::reportDiscarded(InputSectionBase<ELFT> *IS) {
102   if (!Config->PrintGcSections || !IS || IS->Live)
103     return;
104   errs() << "removing unused section from '" << IS->Name << "' in file '"
105          << IS->getFile()->getName() << "'\n";
106 }
107 
108 template <class ELFT> static bool needsInterpSection() {
109   return !Symtab<ELFT>::X->getSharedFiles().empty() &&
110          !Config->DynamicLinker.empty() &&
111          !Script<ELFT>::X->ignoreInterpSection();
112 }
113 
114 template <class ELFT> void elf::writeResult() {
115   typedef typename ELFT::uint uintX_t;
116   typedef typename ELFT::Ehdr Elf_Ehdr;
117 
118   // Create singleton output sections.
119   OutputSection<ELFT> Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
120   DynamicSection<ELFT> Dynamic;
121   EhOutputSection<ELFT> EhFrame;
122   GotSection<ELFT> Got;
123   PltSection<ELFT> Plt;
124   RelocationSection<ELFT> RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn",
125                                   Config->ZCombreloc);
126   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
127   VersionTableSection<ELFT> VerSym;
128   VersionNeedSection<ELFT> VerNeed;
129 
130   OutputSectionBase<ELFT> ElfHeader("", 0, SHF_ALLOC);
131   ElfHeader.setSize(sizeof(Elf_Ehdr));
132   OutputSectionBase<ELFT> ProgramHeaders("", 0, SHF_ALLOC);
133   ProgramHeaders.updateAlignment(sizeof(uintX_t));
134 
135   // Instantiate optional output sections if they are needed.
136   std::unique_ptr<InterpSection<ELFT>> Interp;
137   std::unique_ptr<BuildIdSection<ELFT>> BuildId;
138   std::unique_ptr<StringTableSection<ELFT>> DynStrTab;
139   std::unique_ptr<SymbolTableSection<ELFT>> DynSymTab;
140   std::unique_ptr<EhFrameHeader<ELFT>> EhFrameHdr;
141   std::unique_ptr<GnuHashTableSection<ELFT>> GnuHashTab;
142   std::unique_ptr<GotPltSection<ELFT>> GotPlt;
143   std::unique_ptr<HashTableSection<ELFT>> HashTab;
144   std::unique_ptr<RelocationSection<ELFT>> RelaPlt;
145   std::unique_ptr<StringTableSection<ELFT>> StrTab;
146   std::unique_ptr<SymbolTableSection<ELFT>> SymTabSec;
147   std::unique_ptr<OutputSection<ELFT>> MipsRldMap;
148   std::unique_ptr<VersionDefinitionSection<ELFT>> VerDef;
149 
150   if (needsInterpSection<ELFT>())
151     Interp.reset(new InterpSection<ELFT>);
152 
153   if (Config->BuildId == BuildIdKind::Fnv1)
154     BuildId.reset(new BuildIdFnv1<ELFT>);
155   else if (Config->BuildId == BuildIdKind::Md5)
156     BuildId.reset(new BuildIdMd5<ELFT>);
157   else if (Config->BuildId == BuildIdKind::Sha1)
158     BuildId.reset(new BuildIdSha1<ELFT>);
159   else if (Config->BuildId == BuildIdKind::Uuid)
160     BuildId.reset(new BuildIdUuid<ELFT>);
161   else if (Config->BuildId == BuildIdKind::Hexstring)
162     BuildId.reset(new BuildIdHexstring<ELFT>);
163 
164   if (!Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic) {
165     DynStrTab.reset(new StringTableSection<ELFT>(".dynstr", true));
166     DynSymTab.reset(new SymbolTableSection<ELFT>(*DynStrTab));
167   }
168 
169   if (Config->EhFrameHdr)
170     EhFrameHdr.reset(new EhFrameHeader<ELFT>);
171 
172   if (Config->GnuHash)
173     GnuHashTab.reset(new GnuHashTableSection<ELFT>);
174   if (Config->SysvHash)
175     HashTab.reset(new HashTableSection<ELFT>);
176   StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
177   GotPlt.reset(new GotPltSection<ELFT>);
178   RelaPlt.reset(new RelocationSection<ELFT>(S, false /*Sort*/));
179   if (Config->Strip != StripPolicy::All) {
180     StrTab.reset(new StringTableSection<ELFT>(".strtab", false));
181     SymTabSec.reset(new SymbolTableSection<ELFT>(*StrTab));
182   }
183   if (Config->EMachine == EM_MIPS && !Config->Shared) {
184     // This is a MIPS specific section to hold a space within the data segment
185     // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
186     // See "Dynamic section" in Chapter 5 in the following document:
187     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
188     MipsRldMap.reset(new OutputSection<ELFT>(".rld_map", SHT_PROGBITS,
189                                              SHF_ALLOC | SHF_WRITE));
190     MipsRldMap->setSize(sizeof(uintX_t));
191     MipsRldMap->updateAlignment(sizeof(uintX_t));
192   }
193   if (!Config->VersionDefinitions.empty())
194     VerDef.reset(new VersionDefinitionSection<ELFT>());
195 
196   Out<ELFT>::Bss = &Bss;
197   Out<ELFT>::BuildId = BuildId.get();
198   Out<ELFT>::DynStrTab = DynStrTab.get();
199   Out<ELFT>::DynSymTab = DynSymTab.get();
200   Out<ELFT>::Dynamic = &Dynamic;
201   Out<ELFT>::EhFrame = &EhFrame;
202   Out<ELFT>::EhFrameHdr = EhFrameHdr.get();
203   Out<ELFT>::GnuHashTab = GnuHashTab.get();
204   Out<ELFT>::Got = &Got;
205   Out<ELFT>::GotPlt = GotPlt.get();
206   Out<ELFT>::HashTab = HashTab.get();
207   Out<ELFT>::Interp = Interp.get();
208   Out<ELFT>::Plt = &Plt;
209   Out<ELFT>::RelaDyn = &RelaDyn;
210   Out<ELFT>::RelaPlt = RelaPlt.get();
211   Out<ELFT>::ShStrTab = &ShStrTab;
212   Out<ELFT>::StrTab = StrTab.get();
213   Out<ELFT>::SymTab = SymTabSec.get();
214   Out<ELFT>::VerDef = VerDef.get();
215   Out<ELFT>::VerSym = &VerSym;
216   Out<ELFT>::VerNeed = &VerNeed;
217   Out<ELFT>::MipsRldMap = MipsRldMap.get();
218   Out<ELFT>::Opd = nullptr;
219   Out<ELFT>::OpdBuf = nullptr;
220   Out<ELFT>::TlsPhdr = nullptr;
221   Out<ELFT>::ElfHeader = &ElfHeader;
222   Out<ELFT>::ProgramHeaders = &ProgramHeaders;
223 
224   Out<ELFT>::PreinitArray = nullptr;
225   Out<ELFT>::InitArray = nullptr;
226   Out<ELFT>::FiniArray = nullptr;
227 
228   Writer<ELFT>().run();
229   Out<ELFT>::Pool.clear();
230 }
231 
232 template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
233   std::vector<DefinedCommon *> V;
234   for (Symbol *S : Symtab<ELFT>::X->getSymbols())
235     if (auto *B = dyn_cast<DefinedCommon>(S->body()))
236       V.push_back(B);
237   return V;
238 }
239 
240 // The main function of the writer.
241 template <class ELFT> void Writer<ELFT>::run() {
242   if (Config->Discard != DiscardPolicy::All)
243     copyLocalSymbols();
244   addReservedSymbols();
245 
246   if (Target->NeedsThunks)
247     forEachRelSec(createThunks<ELFT>);
248 
249   CommonInputSection<ELFT> Common(getCommonSymbols<ELFT>());
250   CommonInputSection<ELFT>::X = &Common;
251 
252   Script<ELFT>::X->createAssignments();
253 
254   Script<ELFT>::X->OutputSections = &OutputSections;
255   if (ScriptConfig->HasContents)
256     Script<ELFT>::X->createSections(Factory);
257   else
258     createSections();
259 
260   finalizeSections();
261   if (HasError)
262     return;
263 
264   if (Config->Relocatable) {
265     assignFileOffsets();
266   } else {
267     Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
268                                                 : createPhdrs();
269     fixHeaders();
270     if (ScriptConfig->HasContents) {
271       Script<ELFT>::X->assignAddresses();
272     } else {
273       fixSectionAlignments();
274       assignAddresses();
275     }
276 
277     if (!Config->OFormatBinary)
278       assignFileOffsets();
279     else
280       assignFileOffsetsBinary();
281 
282     setPhdrs();
283     fixAbsoluteSymbols();
284   }
285 
286   openFile();
287   if (HasError)
288     return;
289   if (!Config->OFormatBinary) {
290     writeHeader();
291     writeSections();
292   } else {
293     writeSectionsBinary();
294   }
295   writeBuildId();
296   if (HasError)
297     return;
298   if (auto EC = Buffer->commit())
299     error(EC, "failed to write to the output file");
300 }
301 
302 template <class ELFT> static void reportUndefined(SymbolBody *Sym) {
303   if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
304     return;
305 
306   if (Config->Shared && Sym->symbol()->Visibility == STV_DEFAULT &&
307       Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef)
308     return;
309 
310   std::string Msg = "undefined symbol: " + Sym->getName().str();
311   if (Sym->File)
312     Msg += " in " + getFilename(Sym->File);
313   if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn)
314     warning(Msg);
315   else
316     error(Msg);
317 }
318 
319 template <class ELFT>
320 static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
321                                const SymbolBody &B) {
322   if (B.isFile())
323     return false;
324 
325   // We keep sections in symtab for relocatable output.
326   if (B.isSection())
327     return Config->Relocatable;
328 
329   // If sym references a section in a discarded group, don't keep it.
330   if (Sec == &InputSection<ELFT>::Discarded)
331     return false;
332 
333   if (Config->Discard == DiscardPolicy::None)
334     return true;
335 
336   // In ELF assembly .L symbols are normally discarded by the assembler.
337   // If the assembler fails to do so, the linker discards them if
338   // * --discard-locals is used.
339   // * The symbol is in a SHF_MERGE section, which is normally the reason for
340   //   the assembler keeping the .L symbol.
341   if (!SymName.startswith(".L") && !SymName.empty())
342     return true;
343 
344   if (Config->Discard == DiscardPolicy::Locals)
345     return false;
346 
347   return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
348 }
349 
350 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
351   if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
352     return false;
353 
354   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
355     // Always include absolute symbols.
356     if (!D->Section)
357       return true;
358     // Exclude symbols pointing to garbage-collected sections.
359     if (!D->Section->Live)
360       return false;
361     if (auto *S = dyn_cast<MergeInputSection<ELFT>>(D->Section))
362       if (!S->getSectionPiece(D->Value)->Live)
363         return false;
364   }
365   return true;
366 }
367 
368 // Local symbols are not in the linker's symbol table. This function scans
369 // each object file's symbol table to copy local symbols to the output.
370 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
371   if (!Out<ELFT>::SymTab)
372     return;
373   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
374        Symtab<ELFT>::X->getObjectFiles()) {
375     const char *StrTab = F->getStringTable().data();
376     for (SymbolBody *B : F->getLocalSymbols()) {
377       auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
378       // No reason to keep local undefined symbol in symtab.
379       if (!DR)
380         continue;
381       if (!includeInSymtab<ELFT>(*B))
382         continue;
383       StringRef SymName(StrTab + B->getNameOffset());
384       InputSectionBase<ELFT> *Sec = DR->Section;
385       if (!shouldKeepInSymtab<ELFT>(Sec, SymName, *B))
386         continue;
387       ++Out<ELFT>::SymTab->NumLocals;
388       if (Config->Relocatable)
389         B->DynsymIndex = Out<ELFT>::SymTab->NumLocals;
390       F->KeptLocalSyms.push_back(
391           std::make_pair(DR, Out<ELFT>::SymTab->StrTabSec.addString(SymName)));
392     }
393   }
394 }
395 
396 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
397 // we would like to make sure appear is a specific order to maximize their
398 // coverage by a single signed 16-bit offset from the TOC base pointer.
399 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
400 // sections. This will put it next to the loaded special PPC64 sections (and,
401 // thus, within reach of the TOC base pointer).
402 static int getPPC64SectionRank(StringRef SectionName) {
403   return StringSwitch<int>(SectionName)
404       .Case(".tocbss", 0)
405       .Case(".branch_lt", 2)
406       .Case(".toc", 3)
407       .Case(".toc1", 4)
408       .Case(".opd", 5)
409       .Default(1);
410 }
411 
412 template <class ELFT> bool elf::isRelroSection(OutputSectionBase<ELFT> *Sec) {
413   if (!Config->ZRelro)
414     return false;
415   typename ELFT::uint Flags = Sec->getFlags();
416   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
417     return false;
418   if (Flags & SHF_TLS)
419     return true;
420   uint32_t Type = Sec->getType();
421   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
422       Type == SHT_PREINIT_ARRAY)
423     return true;
424   if (Sec == Out<ELFT>::GotPlt)
425     return Config->ZNow;
426   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
427     return true;
428   StringRef S = Sec->getName();
429   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
430          S == ".eh_frame";
431 }
432 
433 // Output section ordering is determined by this function.
434 template <class ELFT>
435 static bool compareSections(OutputSectionBase<ELFT> *A,
436                             OutputSectionBase<ELFT> *B) {
437   typedef typename ELFT::uint uintX_t;
438 
439   int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName());
440   if (Comp != 0)
441     return Comp < 0;
442 
443   uintX_t AFlags = A->getFlags();
444   uintX_t BFlags = B->getFlags();
445 
446   // Allocatable sections go first to reduce the total PT_LOAD size and
447   // so debug info doesn't change addresses in actual code.
448   bool AIsAlloc = AFlags & SHF_ALLOC;
449   bool BIsAlloc = BFlags & SHF_ALLOC;
450   if (AIsAlloc != BIsAlloc)
451     return AIsAlloc;
452 
453   // We don't have any special requirements for the relative order of
454   // two non allocatable sections.
455   if (!AIsAlloc)
456     return false;
457 
458   // We want the read only sections first so that they go in the PT_LOAD
459   // covering the program headers at the start of the file.
460   bool AIsWritable = AFlags & SHF_WRITE;
461   bool BIsWritable = BFlags & SHF_WRITE;
462   if (AIsWritable != BIsWritable)
463     return BIsWritable;
464 
465   // For a corresponding reason, put non exec sections first (the program
466   // header PT_LOAD is not executable).
467   bool AIsExec = AFlags & SHF_EXECINSTR;
468   bool BIsExec = BFlags & SHF_EXECINSTR;
469   if (AIsExec != BIsExec)
470     return BIsExec;
471 
472   // If we got here we know that both A and B are in the same PT_LOAD.
473 
474   // The TLS initialization block needs to be a single contiguous block in a R/W
475   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
476   // sections are placed here as they don't take up virtual address space in the
477   // PT_LOAD.
478   bool AIsTls = AFlags & SHF_TLS;
479   bool BIsTls = BFlags & SHF_TLS;
480   if (AIsTls != BIsTls)
481     return AIsTls;
482 
483   // The next requirement we have is to put nobits sections last. The
484   // reason is that the only thing the dynamic linker will see about
485   // them is a p_memsz that is larger than p_filesz. Seeing that it
486   // zeros the end of the PT_LOAD, so that has to correspond to the
487   // nobits sections.
488   bool AIsNoBits = A->getType() == SHT_NOBITS;
489   bool BIsNoBits = B->getType() == SHT_NOBITS;
490   if (AIsNoBits != BIsNoBits)
491     return BIsNoBits;
492 
493   // We place RelRo section before plain r/w ones.
494   bool AIsRelRo = isRelroSection(A);
495   bool BIsRelRo = isRelroSection(B);
496   if (AIsRelRo != BIsRelRo)
497     return AIsRelRo;
498 
499   // Some architectures have additional ordering restrictions for sections
500   // within the same PT_LOAD.
501   if (Config->EMachine == EM_PPC64)
502     return getPPC64SectionRank(A->getName()) <
503            getPPC64SectionRank(B->getName());
504 
505   return false;
506 }
507 
508 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
509   return !S || S == &InputSection<ELFT>::Discarded || !S->Live;
510 }
511 
512 // Program header entry
513 template<class ELFT>
514 PhdrEntry<ELFT>::PhdrEntry(unsigned Type, unsigned Flags) {
515   H.p_type = Type;
516   H.p_flags = Flags;
517 }
518 
519 template<class ELFT>
520 void PhdrEntry<ELFT>::add(OutputSectionBase<ELFT> *Sec) {
521   Last = Sec;
522   if (!First)
523     First = Sec;
524   H.p_align = std::max<typename ELFT::uint>(H.p_align, Sec->getAlignment());
525 }
526 
527 template <class ELFT>
528 static Symbol *addOptionalSynthetic(StringRef Name,
529                                     OutputSectionBase<ELFT> *Sec,
530                                     typename ELFT::uint Val) {
531   SymbolBody *S = Symtab<ELFT>::X->find(Name);
532   if (!S)
533     return nullptr;
534   if (!S->isUndefined() && !S->isShared())
535     return S->symbol();
536   return Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, STV_HIDDEN);
537 }
538 
539 template <class ELFT>
540 static void addSynthetic(StringRef Name, OutputSectionBase<ELFT> *Sec,
541                          typename ELFT::uint Val) {
542   SymbolBody *S = Symtab<ELFT>::X->find(Name);
543   if (!S || S->isUndefined() || S->isShared())
544     Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, STV_HIDDEN);
545 }
546 
547 // The beginning and the ending of .rel[a].plt section are marked
548 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
549 // executable. The runtime needs these symbols in order to resolve
550 // all IRELATIVE relocs on startup. For dynamic executables, we don't
551 // need these symbols, since IRELATIVE relocs are resolved through GOT
552 // and PLT. For details, see http://www.airs.com/blog/archives/403.
553 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
554   if (Out<ELFT>::DynSymTab || !Out<ELFT>::RelaPlt)
555     return;
556   StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
557   addOptionalSynthetic(S, Out<ELFT>::RelaPlt, 0);
558 
559   S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
560   addOptionalSynthetic(S, Out<ELFT>::RelaPlt,
561                        DefinedSynthetic<ELFT>::SectionEnd);
562 }
563 
564 // The linker is expected to define some symbols depending on
565 // the linking result. This function defines such symbols.
566 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
567   if (Config->EMachine == EM_MIPS && !Config->Relocatable) {
568     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
569     // so that it points to an absolute address which is relative to GOT.
570     // See "Global Data Symbols" in Chapter 6 in the following document:
571     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
572     Symtab<ELFT>::X->addSynthetic("_gp", Out<ELFT>::Got, MipsGPOffset,
573                                   STV_HIDDEN);
574 
575     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
576     // start of function and 'gp' pointer into GOT.
577     Symbol *Sym =
578         addOptionalSynthetic("_gp_disp", Out<ELFT>::Got, MipsGPOffset);
579     if (Sym)
580       ElfSym<ELFT>::MipsGpDisp = Sym->body();
581 
582     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
583     // pointer. This symbol is used in the code generated by .cpload pseudo-op
584     // in case of using -mno-shared option.
585     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
586     addOptionalSynthetic("__gnu_local_gp", Out<ELFT>::Got, MipsGPOffset);
587   }
588 
589   // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
590   // is magical and is used to produce a R_386_GOTPC relocation.
591   // The R_386_GOTPC relocation value doesn't actually depend on the
592   // symbol value, so it could use an index of STN_UNDEF which, according
593   // to the spec, means the symbol value is 0.
594   // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
595   // the object file.
596   // The situation is even stranger on x86_64 where the assembly doesn't
597   // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
598   // an undefined symbol in the .o files.
599   // Given that the symbol is effectively unused, we just create a dummy
600   // hidden one to avoid the undefined symbol error.
601   if (!Config->Relocatable)
602     Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
603 
604   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
605   // static linking the linker is required to optimize away any references to
606   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
607   // to avoid the undefined symbol error. As usual as special case is MIPS -
608   // MIPS libc defines __tls_get_addr itself because there are no TLS
609   // optimizations for this target.
610   if (!Out<ELFT>::DynSymTab && Config->EMachine != EM_MIPS)
611     Symtab<ELFT>::X->addIgnored("__tls_get_addr");
612 
613   // If linker script do layout we do not need to create any standart symbols.
614   if (ScriptConfig->HasContents)
615     return;
616 
617   ElfSym<ELFT>::EhdrStart = Symtab<ELFT>::X->addIgnored("__ehdr_start");
618 
619   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
620                        DefinedRegular<ELFT> *&Sym2) {
621     Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT);
622 
623     // The name without the underscore is not a reserved name,
624     // so it is defined only when there is a reference against it.
625     assert(S.startswith("_"));
626     S = S.substr(1);
627     if (SymbolBody *B = Symtab<ELFT>::X->find(S))
628       if (B->isUndefined())
629         Sym2 = Symtab<ELFT>::X->addAbsolute(S, STV_DEFAULT);
630   };
631 
632   Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
633   Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2);
634   Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2);
635 }
636 
637 // Sort input sections by section name suffixes for
638 // __attribute__((init_priority(N))).
639 template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) {
640   if (S)
641     reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
642 }
643 
644 // Sort input sections by the special rule for .ctors and .dtors.
645 template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
646   if (S)
647     reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
648 }
649 
650 template <class ELFT>
651 void Writer<ELFT>::forEachRelSec(
652     std::function<void(InputSectionBase<ELFT> &, const typename ELFT::Shdr &)>
653         Fn) {
654   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
655        Symtab<ELFT>::X->getObjectFiles()) {
656     for (InputSectionBase<ELFT> *C : F->getSections()) {
657       if (isDiscarded(C))
658         continue;
659       // Scan all relocations. Each relocation goes through a series
660       // of tests to determine if it needs special treatment, such as
661       // creating GOT, PLT, copy relocations, etc.
662       // Note that relocations for non-alloc sections are directly
663       // processed by InputSection::relocateNonAlloc.
664       if (!(C->getSectionHdr()->sh_flags & SHF_ALLOC))
665         continue;
666       if (auto *S = dyn_cast<InputSection<ELFT>>(C)) {
667         for (const Elf_Shdr *RelSec : S->RelocSections)
668           Fn(*S, *RelSec);
669         continue;
670       }
671       if (auto *S = dyn_cast<EhInputSection<ELFT>>(C))
672         if (S->RelocSection)
673           Fn(*S, *S->RelocSection);
674     }
675   }
676 }
677 
678 template <class ELFT> void Writer<ELFT>::createSections() {
679   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
680        Symtab<ELFT>::X->getObjectFiles()) {
681     for (InputSectionBase<ELFT> *C : F->getSections()) {
682       if (isDiscarded(C)) {
683         reportDiscarded(C);
684         continue;
685       }
686       OutputSectionBase<ELFT> *Sec;
687       bool IsNew;
688       std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C));
689       if (IsNew)
690         OutputSections.push_back(Sec);
691       Sec->addSection(C);
692     }
693   }
694 
695   sortInitFini(findSection(".init_array"));
696   sortInitFini(findSection(".fini_array"));
697   sortCtorsDtors(findSection(".ctors"));
698   sortCtorsDtors(findSection(".dtors"));
699 
700   for (OutputSectionBase<ELFT> *Sec : OutputSections)
701     Sec->assignOffsets();
702 }
703 
704 // Create output section objects and add them to OutputSections.
705 template <class ELFT> void Writer<ELFT>::finalizeSections() {
706   Out<ELFT>::PreinitArray = findSection(".preinit_array");
707   Out<ELFT>::InitArray = findSection(".init_array");
708   Out<ELFT>::FiniArray = findSection(".fini_array");
709 
710   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
711   // symbols for sections, so that the runtime can get the start and end
712   // addresses of each section by section name. Add such symbols.
713   if (!Config->Relocatable) {
714     addStartEndSymbols();
715     for (OutputSectionBase<ELFT> *Sec : OutputSections)
716       addStartStopSymbols(Sec);
717   }
718 
719   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
720   // It should be okay as no one seems to care about the type.
721   // Even the author of gold doesn't remember why gold behaves that way.
722   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
723   if (Out<ELFT>::DynSymTab)
724     Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0,
725                                   STV_HIDDEN);
726 
727   // Define __rel[a]_iplt_{start,end} symbols if needed.
728   addRelIpltSymbols();
729 
730   if (!Out<ELFT>::EhFrame->empty()) {
731     OutputSections.push_back(Out<ELFT>::EhFrame);
732     Out<ELFT>::EhFrame->finalize();
733   }
734 
735   // Scan relocations. This must be done after every symbol is declared so that
736   // we can correctly decide if a dynamic relocation is needed.
737   forEachRelSec(scanRelocations<ELFT>);
738 
739   // Now that we have defined all possible symbols including linker-
740   // synthesized ones. Visit all symbols to give the finishing touches.
741   for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
742     SymbolBody *Body = S->body();
743 
744     // We only report undefined symbols in regular objects. This means that we
745     // will accept an undefined reference in bitcode if it can be optimized out.
746     if (S->IsUsedInRegularObj && Body->isUndefined() && !S->isWeak())
747       reportUndefined<ELFT>(Body);
748 
749     if (!includeInSymtab<ELFT>(*Body))
750       continue;
751     if (Out<ELFT>::SymTab)
752       Out<ELFT>::SymTab->addSymbol(Body);
753 
754     if (Out<ELFT>::DynSymTab && S->includeInDynsym()) {
755       Out<ELFT>::DynSymTab->addSymbol(Body);
756       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
757         if (SS->file()->isNeeded())
758           Out<ELFT>::VerNeed->addSymbol(SS);
759     }
760   }
761 
762   // Do not proceed if there was an undefined symbol.
763   if (HasError)
764     return;
765 
766   // If linker script processor hasn't added common symbol section yet,
767   // then add it to .bss now.
768   if (!CommonInputSection<ELFT>::X->OutSec) {
769     Out<ELFT>::Bss->addSection(CommonInputSection<ELFT>::X);
770     Out<ELFT>::Bss->assignOffsets();
771   }
772 
773   // So far we have added sections from input object files.
774   // This function adds linker-created Out<ELFT>::* sections.
775   addPredefinedSections();
776 
777   std::stable_sort(OutputSections.begin(), OutputSections.end(),
778                    compareSections<ELFT>);
779 
780   unsigned I = 1;
781   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
782     Sec->SectionIndex = I++;
783     Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName()));
784   }
785 
786   // Finalizers fix each section's size.
787   // .dynsym is finalized early since that may fill up .gnu.hash.
788   if (Out<ELFT>::DynSymTab)
789     Out<ELFT>::DynSymTab->finalize();
790 
791   // Fill other section headers. The dynamic table is finalized
792   // at the end because some tags like RELSZ depend on result
793   // of finalizing other sections. The dynamic string table is
794   // finalized once the .dynamic finalizer has added a few last
795   // strings. See DynamicSection::finalize()
796   for (OutputSectionBase<ELFT> *Sec : OutputSections)
797     if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic)
798       Sec->finalize();
799 
800   if (Out<ELFT>::DynSymTab)
801     Out<ELFT>::Dynamic->finalize();
802 
803   // Now that all output offsets are fixed. Finalize mergeable sections
804   // to fix their maps from input offsets to output offsets.
805   for (OutputSectionBase<ELFT> *Sec : OutputSections)
806     Sec->finalizePieces();
807 }
808 
809 template <class ELFT> bool Writer<ELFT>::needsGot() {
810   if (!Out<ELFT>::Got->empty())
811     return true;
812 
813   // We add the .got section to the result for dynamic MIPS target because
814   // its address and properties are mentioned in the .dynamic section.
815   if (Config->EMachine == EM_MIPS && !Config->Relocatable)
816     return true;
817 
818   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
819   // we need to emit a GOT even if it's empty.
820   return Out<ELFT>::Got->HasGotOffRel;
821 }
822 
823 // This function add Out<ELFT>::* sections to OutputSections.
824 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
825   auto Add = [&](OutputSectionBase<ELFT> *C) {
826     if (C)
827       OutputSections.push_back(C);
828   };
829 
830   // A core file does not usually contain unmodified segments except
831   // the first page of the executable. Add the build ID section to beginning of
832   // the file so that the section is included in the first page.
833   if (Out<ELFT>::BuildId)
834     OutputSections.insert(OutputSections.begin(), Out<ELFT>::BuildId);
835 
836   // Add .interp at first because some loaders want to see that section
837   // on the first page of the executable file when loaded into memory.
838   if (Out<ELFT>::Interp)
839     OutputSections.insert(OutputSections.begin(), Out<ELFT>::Interp);
840 
841   // This order is not the same as the final output order
842   // because we sort the sections using their attributes below.
843   Add(Out<ELFT>::SymTab);
844   Add(Out<ELFT>::ShStrTab);
845   Add(Out<ELFT>::StrTab);
846   if (Out<ELFT>::DynSymTab) {
847     Add(Out<ELFT>::DynSymTab);
848 
849     bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
850     if (Out<ELFT>::VerDef || HasVerNeed)
851       Add(Out<ELFT>::VerSym);
852     Add(Out<ELFT>::VerDef);
853     if (HasVerNeed)
854       Add(Out<ELFT>::VerNeed);
855 
856     Add(Out<ELFT>::GnuHashTab);
857     Add(Out<ELFT>::HashTab);
858     Add(Out<ELFT>::Dynamic);
859     Add(Out<ELFT>::DynStrTab);
860     if (Out<ELFT>::RelaDyn->hasRelocs())
861       Add(Out<ELFT>::RelaDyn);
862     Add(Out<ELFT>::MipsRldMap);
863   }
864 
865   // We always need to add rel[a].plt to output if it has entries.
866   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
867   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs())
868     Add(Out<ELFT>::RelaPlt);
869 
870   if (needsGot())
871     Add(Out<ELFT>::Got);
872   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
873     Add(Out<ELFT>::GotPlt);
874   if (!Out<ELFT>::Plt->empty())
875     Add(Out<ELFT>::Plt);
876   if (!Out<ELFT>::EhFrame->empty())
877     Add(Out<ELFT>::EhFrameHdr);
878   if (Out<ELFT>::Bss->getSize() > 0)
879     Add(Out<ELFT>::Bss);
880 }
881 
882 // The linker is expected to define SECNAME_start and SECNAME_end
883 // symbols for a few sections. This function defines them.
884 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
885   auto Define = [&](StringRef Start, StringRef End,
886                     OutputSectionBase<ELFT> *OS) {
887     if (OS) {
888       addSynthetic(Start, OS, 0);
889       addSynthetic(End, OS, DefinedSynthetic<ELFT>::SectionEnd);
890     } else {
891       addOptionalSynthetic(Start, (OutputSectionBase<ELFT> *)nullptr, 0);
892       addOptionalSynthetic(End, (OutputSectionBase<ELFT> *)nullptr, 0);
893     }
894   };
895 
896   Define("__preinit_array_start", "__preinit_array_end",
897          Out<ELFT>::PreinitArray);
898   Define("__init_array_start", "__init_array_end", Out<ELFT>::InitArray);
899   Define("__fini_array_start", "__fini_array_end", Out<ELFT>::FiniArray);
900 }
901 
902 // If a section name is valid as a C identifier (which is rare because of
903 // the leading '.'), linkers are expected to define __start_<secname> and
904 // __stop_<secname> symbols. They are at beginning and end of the section,
905 // respectively. This is not requested by the ELF standard, but GNU ld and
906 // gold provide the feature, and used by many programs.
907 template <class ELFT>
908 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
909   StringRef S = Sec->getName();
910   if (!isValidCIdentifier(S))
911     return;
912   StringSaver Saver(Alloc);
913   StringRef Start = Saver.save("__start_" + S);
914   StringRef Stop = Saver.save("__stop_" + S);
915   if (SymbolBody *B = Symtab<ELFT>::X->find(Start))
916     if (B->isUndefined())
917       Symtab<ELFT>::X->addSynthetic(Start, Sec, 0, B->getVisibility());
918   if (SymbolBody *B = Symtab<ELFT>::X->find(Stop))
919     if (B->isUndefined())
920       Symtab<ELFT>::X->addSynthetic(
921           Stop, Sec, DefinedSynthetic<ELFT>::SectionEnd, B->getVisibility());
922 }
923 
924 template <class ELFT>
925 OutputSectionBase<ELFT> *Writer<ELFT>::findSection(StringRef Name) {
926   for (OutputSectionBase<ELFT> *Sec : OutputSections)
927     if (Sec->getName() == Name)
928       return Sec;
929   return nullptr;
930 }
931 
932 template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) {
933   if (!(Sec->getFlags() & SHF_ALLOC))
934     return false;
935 
936   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
937   // responsible for allocating space for them, not the PT_LOAD that
938   // contains the TLS initialization image.
939   if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS)
940     return false;
941   return true;
942 }
943 
944 // Decide which program headers to create and which sections to include in each
945 // one.
946 template <class ELFT>
947 std::vector<PhdrEntry<ELFT>> Writer<ELFT>::createPhdrs() {
948   std::vector<Phdr> Ret;
949 
950   auto AddHdr = [&](unsigned Type, unsigned Flags) -> Phdr * {
951     Ret.emplace_back(Type, Flags);
952     return &Ret.back();
953   };
954 
955   // The first phdr entry is PT_PHDR which describes the program header itself.
956   Phdr &Hdr = *AddHdr(PT_PHDR, PF_R);
957   Hdr.add(Out<ELFT>::ProgramHeaders);
958 
959   // PT_INTERP must be the second entry if exists.
960   if (Out<ELFT>::Interp) {
961     Phdr &Hdr = *AddHdr(PT_INTERP, Out<ELFT>::Interp->getPhdrFlags());
962     Hdr.add(Out<ELFT>::Interp);
963   }
964 
965   // Add the first PT_LOAD segment for regular output sections.
966   uintX_t Flags = PF_R;
967   Phdr *Load = AddHdr(PT_LOAD, Flags);
968   Load->add(Out<ELFT>::ElfHeader);
969   Load->add(Out<ELFT>::ProgramHeaders);
970 
971   Phdr TlsHdr(PT_TLS, PF_R);
972   Phdr RelRo(PT_GNU_RELRO, PF_R);
973   Phdr Note(PT_NOTE, PF_R);
974   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
975     if (!(Sec->getFlags() & SHF_ALLOC))
976       break;
977 
978     // If we meet TLS section then we create TLS header
979     // and put all TLS sections inside for futher use when
980     // assign addresses.
981     if (Sec->getFlags() & SHF_TLS)
982       TlsHdr.add(Sec);
983 
984     if (!needsPtLoad(Sec))
985       continue;
986 
987     // Segments are contiguous memory regions that has the same attributes
988     // (e.g. executable or writable). There is one phdr for each segment.
989     // Therefore, we need to create a new phdr when the next section has
990     // different flags or is loaded at a discontiguous address using AT linker
991     // script command.
992     uintX_t NewFlags = Sec->getPhdrFlags();
993     if (Script<ELFT>::X->getLma(Sec->getName()) || Flags != NewFlags) {
994       Load = AddHdr(PT_LOAD, NewFlags);
995       Flags = NewFlags;
996     }
997 
998     Load->add(Sec);
999 
1000     if (isRelroSection(Sec))
1001       RelRo.add(Sec);
1002     if (Sec->getType() == SHT_NOTE)
1003       Note.add(Sec);
1004   }
1005 
1006   // Add the TLS segment unless it's empty.
1007   if (TlsHdr.First)
1008     Ret.push_back(std::move(TlsHdr));
1009 
1010   // Add an entry for .dynamic.
1011   if (Out<ELFT>::DynSymTab) {
1012     Phdr &H = *AddHdr(PT_DYNAMIC, Out<ELFT>::Dynamic->getPhdrFlags());
1013     H.add(Out<ELFT>::Dynamic);
1014   }
1015 
1016   // PT_GNU_RELRO includes all sections that should be marked as
1017   // read-only by dynamic linker after proccessing relocations.
1018   if (RelRo.First)
1019     Ret.push_back(std::move(RelRo));
1020 
1021   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1022   if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
1023     Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME, Out<ELFT>::EhFrameHdr->getPhdrFlags());
1024     Hdr.add(Out<ELFT>::EhFrameHdr);
1025   }
1026 
1027   // PT_GNU_STACK is a special section to tell the loader to make the
1028   // pages for the stack non-executable.
1029   if (!Config->ZExecStack) {
1030     Phdr &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W);
1031     if (Config->ZStackSize != uint64_t(-1))
1032       Hdr.H.p_memsz = Config->ZStackSize;
1033   }
1034 
1035   if (Note.First)
1036     Ret.push_back(std::move(Note));
1037   return Ret;
1038 }
1039 
1040 // The first section of each PT_LOAD and the first section after PT_GNU_RELRO
1041 // have to be page aligned so that the dynamic linker can set the permissions.
1042 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1043   for (const Phdr &P : Phdrs)
1044     if (P.H.p_type == PT_LOAD)
1045       P.First->PageAlign = true;
1046 
1047   for (const Phdr &P : Phdrs) {
1048     if (P.H.p_type != PT_GNU_RELRO)
1049       continue;
1050     // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1051     // have to align it to a page.
1052     auto End = OutputSections.end();
1053     auto I = std::find(OutputSections.begin(), End, P.Last);
1054     if (I == End || (I + 1) == End)
1055       continue;
1056     OutputSectionBase<ELFT> *Sec = *(I + 1);
1057     if (needsPtLoad(Sec))
1058       Sec->PageAlign = true;
1059   }
1060 }
1061 
1062 // We should set file offsets and VAs for elf header and program headers
1063 // sections. These are special, we do not include them into output sections
1064 // list, but have them to simplify the code.
1065 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1066   uintX_t BaseVA = ScriptConfig->HasContents ? 0 : Config->ImageBase;
1067   Out<ELFT>::ElfHeader->setVA(BaseVA);
1068   uintX_t Off = Out<ELFT>::ElfHeader->getSize();
1069   Out<ELFT>::ProgramHeaders->setVA(Off + BaseVA);
1070   Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size());
1071 }
1072 
1073 // Assign VAs (addresses at run-time) to output sections.
1074 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1075   uintX_t VA = Config->ImageBase;
1076   if (!Config->OFormatBinary)
1077     VA +=
1078         Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
1079 
1080   uintX_t ThreadBssOffset = 0;
1081   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1082     uintX_t Alignment = Sec->getAlignment();
1083     if (Sec->PageAlign)
1084       Alignment = std::max<uintX_t>(Alignment, Target->PageSize);
1085 
1086     // We only assign VAs to allocated sections.
1087     if (needsPtLoad(Sec)) {
1088       VA = alignTo(VA, Alignment);
1089       Sec->setVA(VA);
1090       VA += Sec->getSize();
1091     } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) {
1092       uintX_t TVA = VA + ThreadBssOffset;
1093       TVA = alignTo(TVA, Alignment);
1094       Sec->setVA(TVA);
1095       ThreadBssOffset = TVA - VA + Sec->getSize();
1096     }
1097   }
1098 }
1099 
1100 // Adjusts the file alignment for a given output section and returns
1101 // its new file offset. The file offset must be the same with its
1102 // virtual address (modulo the page size) so that the loader can load
1103 // executables without any address adjustment.
1104 template <class ELFT, class uintX_t>
1105 static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) {
1106   uintX_t Alignment = Sec->getAlignment();
1107   if (Sec->PageAlign)
1108     Alignment = std::max<uintX_t>(Alignment, Target->PageSize);
1109   Off = alignTo(Off, Alignment);
1110 
1111   // Relocatable output does not have program headers
1112   // and does not need any other offset adjusting.
1113   if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC))
1114     return Off;
1115   return alignTo(Off, Target->PageSize, Sec->getVA());
1116 }
1117 
1118 template <class ELFT, class uintX_t>
1119 void setOffset(OutputSectionBase<ELFT> *Sec, uintX_t &Off) {
1120   if (Sec->getType() == SHT_NOBITS) {
1121     Sec->setFileOffset(Off);
1122     return;
1123   }
1124 
1125   Off = getFileAlignment<ELFT>(Off, Sec);
1126   Sec->setFileOffset(Off);
1127   Off += Sec->getSize();
1128 }
1129 
1130 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1131   uintX_t Off = 0;
1132   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1133     if (Sec->getFlags() & SHF_ALLOC)
1134       setOffset(Sec, Off);
1135   FileSize = alignTo(Off, sizeof(uintX_t));
1136 }
1137 
1138 // Assign file offsets to output sections.
1139 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1140   uintX_t Off = 0;
1141   setOffset(Out<ELFT>::ElfHeader, Off);
1142   setOffset(Out<ELFT>::ProgramHeaders, Off);
1143 
1144   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1145     setOffset(Sec, Off);
1146 
1147   SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
1148   FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1149 }
1150 
1151 // Finalize the program headers. We call this function after we assign
1152 // file offsets and VAs to all sections.
1153 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1154   for (Phdr &P : Phdrs) {
1155     Elf_Phdr &H = P.H;
1156     OutputSectionBase<ELFT> *First = P.First;
1157     OutputSectionBase<ELFT> *Last = P.Last;
1158     if (First) {
1159       H.p_filesz = Last->getFileOff() - First->getFileOff();
1160       if (Last->getType() != SHT_NOBITS)
1161         H.p_filesz += Last->getSize();
1162       H.p_memsz = Last->getVA() + Last->getSize() - First->getVA();
1163       H.p_offset = First->getFileOff();
1164       H.p_vaddr = First->getVA();
1165     }
1166     if (H.p_type == PT_LOAD)
1167       H.p_align = Target->PageSize;
1168     else if (H.p_type == PT_GNU_RELRO)
1169       H.p_align = 1;
1170 
1171     if (!P.HasLMA) {
1172     // The p_paddr field can be set using linker script AT command.
1173     // By default, it is the same value as p_vaddr.
1174       H.p_paddr = H.p_vaddr;
1175       if (H.p_type == PT_LOAD && First)
1176         if (Expr LmaExpr = Script<ELFT>::X->getLma(First->getName()))
1177           H.p_paddr = LmaExpr(H.p_vaddr);
1178     }
1179 
1180     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1181     // so round up the size to make sure the offsets are correct.
1182     if (H.p_type == PT_TLS) {
1183       Out<ELFT>::TlsPhdr = &H;
1184       if (H.p_memsz)
1185         H.p_memsz = alignTo(H.p_memsz, H.p_align);
1186     }
1187   }
1188 }
1189 
1190 template <class ELFT> static typename ELFT::uint getEntryAddr() {
1191   if (Symbol *S = Config->EntrySym)
1192     return S->body()->getVA<ELFT>();
1193   return Config->EntryAddr;
1194 }
1195 
1196 template <class ELFT> static uint8_t getELFEncoding() {
1197   if (ELFT::TargetEndianness == llvm::support::little)
1198     return ELFDATA2LSB;
1199   return ELFDATA2MSB;
1200 }
1201 
1202 static uint16_t getELFType() {
1203   if (Config->Pic)
1204     return ET_DYN;
1205   if (Config->Relocatable)
1206     return ET_REL;
1207   return ET_EXEC;
1208 }
1209 
1210 // This function is called after we have assigned address and size
1211 // to each section. This function fixes some predefined absolute
1212 // symbol values that depend on section address and size.
1213 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1214   // __ehdr_start is the location of program headers.
1215   if (ElfSym<ELFT>::EhdrStart)
1216     ElfSym<ELFT>::EhdrStart->Value = Out<ELFT>::ProgramHeaders->getVA();
1217 
1218   auto Set = [](DefinedRegular<ELFT> *S1, DefinedRegular<ELFT> *S2, uintX_t V) {
1219     if (S1)
1220       S1->Value = V;
1221     if (S2)
1222       S2->Value = V;
1223   };
1224 
1225   // _etext is the first location after the last read-only loadable segment.
1226   // _edata is the first location after the last read-write loadable segment.
1227   // _end is the first location after the uninitialized data region.
1228   for (Phdr &P : Phdrs) {
1229     Elf_Phdr &H = P.H;
1230     if (H.p_type != PT_LOAD)
1231       continue;
1232     Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, H.p_vaddr + H.p_memsz);
1233 
1234     uintX_t Val = H.p_vaddr + H.p_filesz;
1235     if (H.p_flags & PF_W)
1236       Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val);
1237     else
1238       Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val);
1239   }
1240 }
1241 
1242 template <class ELFT> void Writer<ELFT>::writeHeader() {
1243   uint8_t *Buf = Buffer->getBufferStart();
1244   memcpy(Buf, "\177ELF", 4);
1245 
1246   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1247 
1248   // Write the ELF header.
1249   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1250   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1251   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1252   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1253   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1254   EHdr->e_type = getELFType();
1255   EHdr->e_machine = FirstObj.EMachine;
1256   EHdr->e_version = EV_CURRENT;
1257   EHdr->e_entry = getEntryAddr<ELFT>();
1258   EHdr->e_shoff = SectionHeaderOff;
1259   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1260   EHdr->e_phnum = Phdrs.size();
1261   EHdr->e_shentsize = sizeof(Elf_Shdr);
1262   EHdr->e_shnum = OutputSections.size() + 1;
1263   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1264 
1265   if (Config->EMachine == EM_MIPS)
1266     EHdr->e_flags = getMipsEFlags<ELFT>();
1267 
1268   if (!Config->Relocatable) {
1269     EHdr->e_phoff = sizeof(Elf_Ehdr);
1270     EHdr->e_phentsize = sizeof(Elf_Phdr);
1271   }
1272 
1273   // Write the program header table.
1274   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1275   for (Phdr &P : Phdrs)
1276     *HBuf++ = P.H;
1277 
1278   // Write the section header table. Note that the first table entry is null.
1279   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1280   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1281     Sec->writeHeaderTo(++SHdrs);
1282 }
1283 
1284 template <class ELFT> void Writer<ELFT>::openFile() {
1285   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1286       FileOutputBuffer::create(Config->OutputFile, FileSize,
1287                                FileOutputBuffer::F_executable);
1288   if (auto EC = BufferOrErr.getError())
1289     error(EC, "failed to open " + Config->OutputFile);
1290   else
1291     Buffer = std::move(*BufferOrErr);
1292 }
1293 
1294 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1295   uint8_t *Buf = Buffer->getBufferStart();
1296   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1297     if (Sec->getFlags() & SHF_ALLOC)
1298       Sec->writeTo(Buf + Sec->getFileOff());
1299 }
1300 
1301 // Write section contents to a mmap'ed file.
1302 template <class ELFT> void Writer<ELFT>::writeSections() {
1303   uint8_t *Buf = Buffer->getBufferStart();
1304 
1305   // PPC64 needs to process relocations in the .opd section
1306   // before processing relocations in code-containing sections.
1307   Out<ELFT>::Opd = findSection(".opd");
1308   if (Out<ELFT>::Opd) {
1309     Out<ELFT>::OpdBuf = Buf + Out<ELFT>::Opd->getFileOff();
1310     Out<ELFT>::Opd->writeTo(Buf + Out<ELFT>::Opd->getFileOff());
1311   }
1312 
1313   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1314     if (Sec != Out<ELFT>::Opd && Sec != Out<ELFT>::EhFrameHdr)
1315       Sec->writeTo(Buf + Sec->getFileOff());
1316 
1317   // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1318   // it should be written after .eh_frame is written.
1319   if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr)
1320     Out<ELFT>::EhFrameHdr->writeTo(Buf + Out<ELFT>::EhFrameHdr->getFileOff());
1321 }
1322 
1323 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1324   if (!Out<ELFT>::BuildId)
1325     return;
1326 
1327   // Compute a hash of all sections of the output file.
1328   uint8_t *Start = Buffer->getBufferStart();
1329   uint8_t *End = Start + FileSize;
1330   Out<ELFT>::BuildId->writeBuildId({Start, End});
1331 }
1332 
1333 template void elf::writeResult<ELF32LE>();
1334 template void elf::writeResult<ELF32BE>();
1335 template void elf::writeResult<ELF64LE>();
1336 template void elf::writeResult<ELF64BE>();
1337 
1338 template struct elf::PhdrEntry<ELF32LE>;
1339 template struct elf::PhdrEntry<ELF32BE>;
1340 template struct elf::PhdrEntry<ELF64LE>;
1341 template struct elf::PhdrEntry<ELF64BE>;
1342 
1343 template bool elf::isRelroSection<ELF32LE>(OutputSectionBase<ELF32LE> *);
1344 template bool elf::isRelroSection<ELF32BE>(OutputSectionBase<ELF32BE> *);
1345 template bool elf::isRelroSection<ELF64LE>(OutputSectionBase<ELF64LE> *);
1346 template bool elf::isRelroSection<ELF64BE>(OutputSectionBase<ELF64BE> *);
1347 
1348 template StringRef elf::getOutputSectionName<ELF32LE>(InputSectionBase<ELF32LE> *);
1349 template StringRef elf::getOutputSectionName<ELF32BE>(InputSectionBase<ELF32BE> *);
1350 template StringRef elf::getOutputSectionName<ELF64LE>(InputSectionBase<ELF64LE> *);
1351 template StringRef elf::getOutputSectionName<ELF64BE>(InputSectionBase<ELF64BE> *);
1352 
1353 template void elf::reportDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
1354 template void elf::reportDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
1355 template void elf::reportDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
1356 template void elf::reportDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
1357