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