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