xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision d2793a03)
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 "OutputSections.h"
13 #include "SymbolTable.h"
14 #include "Target.h"
15 
16 #include "llvm/Support/FileOutputBuffer.h"
17 
18 using namespace llvm;
19 using namespace llvm::ELF;
20 using namespace llvm::object;
21 
22 using namespace lld;
23 using namespace lld::elf2;
24 
25 static const int PageSize = 4096;
26 
27 // On freebsd x86_64 the first page cannot be mmaped.
28 // On linux that is controled by vm.mmap_min_addr. At least on some x86_64
29 // installs that is 65536, so the first 15 pages cannot be used.
30 // Given that, the smallest value that can be used in here is 0x10000.
31 // If using 2MB pages, the smallest page aligned address that works is
32 // 0x200000, but it looks like every OS uses 4k pages for executables.
33 // FIXME: This is architecture and OS dependent.
34 static const int VAStart = 0x10000;
35 
36 namespace {
37 
38 static uint32_t toPHDRFlags(uint64_t Flags) {
39   uint32_t Ret = PF_R;
40   if (Flags & SHF_WRITE)
41     Ret |= PF_W;
42   if (Flags & SHF_EXECINSTR)
43     Ret |= PF_X;
44   return Ret;
45 }
46 
47 template <class ELFT> struct ProgramHeader {
48   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
49   typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
50 
51   ProgramHeader(uintX_t Type, uintX_t Flags, uintX_t FileOff, uintX_t VA) {
52     std::memset(&Header, 0, sizeof(Elf_Phdr));
53     Header.p_type = Type;
54     Header.p_flags = Flags;
55     Header.p_align = PageSize;
56     Header.p_offset = FileOff;
57     Header.p_vaddr = VA;
58     Header.p_paddr = VA;
59   }
60 
61   void setValuesFromSection(OutputSectionBase<ELFT::Is64Bits> &Sec) {
62     Header.p_flags = toPHDRFlags(Sec.getFlags());
63     Header.p_offset = Sec.getFileOff();
64     Header.p_vaddr = Sec.getVA();
65     Header.p_paddr = Header.p_vaddr;
66     Header.p_filesz = Sec.getSize();
67     Header.p_memsz = Header.p_filesz;
68     Header.p_align = Sec.getAlign();
69   }
70 
71   Elf_Phdr Header;
72   bool Closed = false;
73 };
74 
75 // The writer writes a SymbolTable result to a file.
76 template <class ELFT> class Writer {
77 public:
78   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
79   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
80   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
81   typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
82   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
83   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
84   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
85   Writer(SymbolTable *T)
86       : SymTabSec(*T, StrTabSec, BssSec), DynSymSec(*T, DynStrSec, BssSec),
87         RelaDynSec(DynSymSec, GotSec, BssSec, T->shouldUseRela()),
88         PltSec(GotSec), HashSec(DynSymSec),
89         DynamicSec(*T, HashSec, RelaDynSec, BssSec),
90         BssSec(PltSec, GotSec, BssSec, ".bss", SHT_NOBITS,
91                SHF_ALLOC | SHF_WRITE) {}
92   void run();
93 
94 private:
95   void createSections();
96   template <bool isRela>
97   void scanRelocs(const InputSection<ELFT> &C,
98                   iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels);
99   void scanRelocs(const InputSection<ELFT> &C);
100   void assignAddresses();
101   void openFile(StringRef OutputPath);
102   void writeHeader();
103   void writeSections();
104   bool needsInterpSection() const {
105     return !SymTabSec.getSymTable().getSharedFiles().empty() &&
106            !Config->DynamicLinker.empty();
107   }
108   bool needsDynamicSections() const {
109     return !SymTabSec.getSymTable().getSharedFiles().empty() || Config->Shared;
110   }
111   unsigned getVAStart() const { return Config->Shared ? 0 : VAStart; }
112 
113   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
114 
115   llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc;
116   std::vector<OutputSectionBase<ELFT::Is64Bits> *> OutputSections;
117   unsigned getNumSections() const { return OutputSections.size() + 1; }
118 
119   llvm::BumpPtrAllocator PAlloc;
120   std::vector<ProgramHeader<ELFT> *> PHDRs;
121   ProgramHeader<ELFT> FileHeaderPHDR{PT_LOAD, PF_R, 0, 0};
122   ProgramHeader<ELFT> InterpPHDR{PT_INTERP, 0, 0, 0};
123   ProgramHeader<ELFT> DynamicPHDR{PT_DYNAMIC, 0, 0, 0};
124 
125   uintX_t FileSize;
126   uintX_t ProgramHeaderOff;
127   uintX_t SectionHeaderOff;
128 
129   StringTableSection<ELFT::Is64Bits> StrTabSec = { /*dynamic=*/false };
130   StringTableSection<ELFT::Is64Bits> DynStrSec = { /*dynamic=*/true };
131 
132   lld::elf2::SymbolTableSection<ELFT> SymTabSec;
133   lld::elf2::SymbolTableSection<ELFT> DynSymSec;
134 
135   RelocationSection<ELFT> RelaDynSec;
136 
137   GotSection<ELFT> GotSec;
138   PltSection<ELFT> PltSec;
139 
140   HashTableSection<ELFT> HashSec;
141 
142   DynamicSection<ELFT> DynamicSec;
143 
144   InterpSection<ELFT::Is64Bits> InterpSec;
145 
146   OutputSection<ELFT> BssSec;
147 };
148 } // anonymous namespace
149 
150 namespace lld {
151 namespace elf2 {
152 
153 template <class ELFT>
154 void writeResult(SymbolTable *Symtab) { Writer<ELFT>(Symtab).run(); }
155 
156 template void writeResult<ELF32LE>(SymbolTable *);
157 template void writeResult<ELF32BE>(SymbolTable *);
158 template void writeResult<ELF64LE>(SymbolTable *);
159 template void writeResult<ELF64BE>(SymbolTable *);
160 
161 } // namespace elf2
162 } // namespace lld
163 
164 // The main function of the writer.
165 template <class ELFT> void Writer<ELFT>::run() {
166   createSections();
167   assignAddresses();
168   openFile(Config->OutputFile);
169   writeHeader();
170   writeSections();
171   error(Buffer->commit());
172 }
173 
174 namespace {
175 template <bool Is64Bits> struct SectionKey {
176   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
177   StringRef Name;
178   uint32_t Type;
179   uintX_t Flags;
180 };
181 }
182 namespace llvm {
183 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
184   static SectionKey<Is64Bits> getEmptyKey() {
185     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
186   }
187   static SectionKey<Is64Bits> getTombstoneKey() {
188     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
189                                 0};
190   }
191   static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
192     return hash_combine(Val.Name, Val.Type, Val.Flags);
193   }
194   static bool isEqual(const SectionKey<Is64Bits> &LHS,
195                       const SectionKey<Is64Bits> &RHS) {
196     return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
197            LHS.Type == RHS.Type && LHS.Flags == RHS.Flags;
198   }
199 };
200 }
201 
202 // The reason we have to do this early scan is as follows
203 // * To mmap the output file, we need to know the size
204 // * For that, we need to know how many dynamic relocs we will have.
205 // It might be possible to avoid this by outputting the file with write:
206 // * Write the allocated output sections, computing addresses.
207 // * Apply relocations, recording which ones require a dynamic reloc.
208 // * Write the dynamic relocations.
209 // * Write the rest of the file.
210 template <class ELFT>
211 template <bool isRela>
212 void Writer<ELFT>::scanRelocs(
213     const InputSection<ELFT> &C,
214     iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
215   typedef Elf_Rel_Impl<ELFT, isRela> RelType;
216   const ObjectFile<ELFT> &File = *C.getFile();
217   bool IsMips64EL = File.getObj().isMips64EL();
218   for (const RelType &RI : Rels) {
219     uint32_t SymIndex = RI.getSymbol(IsMips64EL);
220     SymbolBody *Body = File.getSymbolBody(SymIndex);
221     uint32_t Type = RI.getType(IsMips64EL);
222     if (Body) {
223       if (Target->relocNeedsPlt(Type, *Body)) {
224         if (Body->isInPlt())
225           continue;
226         PltSec.addEntry(Body);
227       }
228       if (Target->relocNeedsGot(Type, *Body)) {
229         if (Body->isInGot())
230           continue;
231         GotSec.addEntry(Body);
232         Body->setUsedInDynamicReloc();
233         RelaDynSec.addReloc({C, RI});
234         continue;
235       }
236       if (Body->isShared()) {
237         Body->setUsedInDynamicReloc();
238         RelaDynSec.addReloc({C, RI});
239         continue;
240       }
241     }
242     if (Config->Shared && !Target->isRelRelative(Type))
243       RelaDynSec.addReloc({C, RI});
244   }
245 }
246 
247 template <class ELFT>
248 void Writer<ELFT>::scanRelocs(const InputSection<ELFT> &C) {
249   ObjectFile<ELFT> *File = C.getFile();
250   ELFFile<ELFT> &EObj = File->getObj();
251 
252   if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
253     return;
254 
255   for (const Elf_Shdr *RelSec : C.RelocSections) {
256     if (RelSec->sh_type == SHT_RELA)
257       scanRelocs(C, EObj.relas(RelSec));
258     else
259       scanRelocs(C, EObj.rels(RelSec));
260   }
261 }
262 
263 template <class ELFT>
264 static void reportUndefined(const SymbolTable &S, const SymbolBody &Sym) {
265   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
266   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
267 
268   if (Config->Shared && !Config->NoUndefined)
269     return;
270 
271   const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym;
272   ELFFileBase *SymFile = nullptr;
273 
274   for (const std::unique_ptr<ObjectFileBase> &F : S.getObjectFiles()) {
275     const auto &File = cast<ObjectFile<ELFT>>(*F);
276     Elf_Sym_Range Syms = File.getObj().symbols(File.getSymbolTable());
277     if (&SymE > Syms.begin() && &SymE < Syms.end())
278       SymFile = F.get();
279   }
280 
281   std::string Message = "undefined symbol: " + Sym.getName().str();
282   if (SymFile)
283     Message += " in " + SymFile->getName().str();
284   if (Config->NoInhibitExec)
285     warning(Message);
286   else
287     error(Message);
288 }
289 
290 // Create output section objects and add them to OutputSections.
291 template <class ELFT> void Writer<ELFT>::createSections() {
292   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map;
293 
294   OutputSections.push_back(&BssSec);
295   Map[{BssSec.getName(), BssSec.getType(), BssSec.getFlags()}] = &BssSec;
296 
297   SymbolTable &Symtab = SymTabSec.getSymTable();
298   for (const std::unique_ptr<ObjectFileBase> &FileB : Symtab.getObjectFiles()) {
299     auto &File = cast<ObjectFile<ELFT>>(*FileB);
300     if (!Config->DiscardAll) {
301       Elf_Sym_Range Syms = File.getLocalSymbols();
302       for (const Elf_Sym &Sym : Syms) {
303         ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
304         if (SymName && shouldKeepInSymtab<ELFT>(*SymName, Sym))
305           SymTabSec.addSymbol(*SymName, true);
306       }
307     }
308     for (InputSection<ELFT> *C : File.getSections()) {
309       if (!C)
310         continue;
311       const Elf_Shdr *H = C->getSectionHdr();
312       SectionKey<ELFT::Is64Bits> Key{C->getSectionName(), H->sh_type,
313                                      H->sh_flags};
314       OutputSection<ELFT> *&Sec = Map[Key];
315       if (!Sec) {
316         Sec = new (CAlloc.Allocate()) OutputSection<ELFT>(
317             PltSec, GotSec, BssSec, Key.Name, Key.Type, Key.Flags);
318         OutputSections.push_back(Sec);
319       }
320       Sec->addSection(C);
321       scanRelocs(*C);
322     }
323   }
324 
325   DynamicSec.PreInitArraySec =
326       Map.lookup({".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC});
327   DynamicSec.InitArraySec =
328       Map.lookup({".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC});
329   DynamicSec.FiniArraySec =
330       Map.lookup({".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC});
331 
332   if (OutputSection<ELFT> *OS = DynamicSec.InitArraySec) {
333     Symtab.addSyntheticSym<ELFT>("__init_array_start", *OS, 0);
334     Symtab.addSyntheticSym<ELFT>("__init_array_end", *OS, OS->getSize());
335   } else {
336     Symtab.addIgnoredSym<ELFT>("__init_array_start");
337     Symtab.addIgnoredSym<ELFT>("__init_array_end");
338   }
339 
340   // FIXME: Try to avoid the extra walk over all global symbols.
341   std::vector<DefinedCommon<ELFT> *> CommonSymbols;
342   for (auto &P : Symtab.getSymbols()) {
343     StringRef Name = P.first;
344     SymbolBody *Body = P.second->Body;
345     if (auto *U = dyn_cast<Undefined<ELFT>>(Body)) {
346       if (!U->isWeak() && !U->canKeepUndefined())
347         reportUndefined<ELFT>(Symtab, *Body);
348     }
349 
350     if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
351       CommonSymbols.push_back(C);
352     if (!includeInSymtab<ELFT>(*Body))
353       continue;
354     SymTabSec.addSymbol(Name);
355 
356     if (needsDynamicSections() && includeInDynamicSymtab(*Body))
357       HashSec.addSymbol(Body);
358   }
359 
360   // Sort the common symbols by alignment as an heuristic to pack them better.
361   std::stable_sort(
362       CommonSymbols.begin(), CommonSymbols.end(),
363       [](const DefinedCommon<ELFT> *A, const DefinedCommon<ELFT> *B) {
364         return A->MaxAlignment > B->MaxAlignment;
365       });
366 
367   uintX_t Off = BssSec.getSize();
368   for (DefinedCommon<ELFT> *C : CommonSymbols) {
369     const Elf_Sym &Sym = C->Sym;
370     uintX_t Align = C->MaxAlignment;
371     Off = RoundUpToAlignment(Off, Align);
372     C->OffsetInBSS = Off;
373     Off += Sym.st_size;
374   }
375 
376   BssSec.setSize(Off);
377 
378   OutputSections.push_back(&SymTabSec);
379 
380   if (needsDynamicSections()) {
381     if (needsInterpSection())
382       OutputSections.push_back(&InterpSec);
383     OutputSections.push_back(&DynSymSec);
384     OutputSections.push_back(&HashSec);
385     OutputSections.push_back(&DynamicSec);
386     OutputSections.push_back(&DynStrSec);
387     if (RelaDynSec.hasRelocs())
388       OutputSections.push_back(&RelaDynSec);
389   }
390   if (!GotSec.empty())
391     OutputSections.push_back(&GotSec);
392   if (!PltSec.empty())
393     OutputSections.push_back(&PltSec);
394 
395   std::stable_sort(
396       OutputSections.begin(), OutputSections.end(),
397       [](OutputSectionBase<ELFT::Is64Bits> *A,
398          OutputSectionBase<ELFT::Is64Bits> *B) {
399         uintX_t AFlags = A->getFlags();
400         uintX_t BFlags = B->getFlags();
401 
402         // Allocatable sections go first to reduce the total PT_LOAD size and
403         // so debug info doesn't change addresses in actual code.
404         bool AIsAlloc = AFlags & SHF_ALLOC;
405         bool BIsAlloc = BFlags & SHF_ALLOC;
406         if (AIsAlloc != BIsAlloc)
407           return AIsAlloc;
408 
409         // We don't have any special requirements for the relative order of
410         // two non allocatable sections.
411         if (!AIsAlloc)
412           return false;
413 
414         // We want the read only sections first so that they go in the PT_LOAD
415         // covering the program headers at the start of the file.
416         bool AIsWritable = AFlags & SHF_WRITE;
417         bool BIsWritable = BFlags & SHF_WRITE;
418         if (AIsWritable != BIsWritable)
419           return BIsWritable;
420 
421         // For a corresponding reason, put non exec sections first (the program
422         // header PT_LOAD is not executable).
423         bool AIsExec = AFlags & SHF_EXECINSTR;
424         bool BIsExec = BFlags & SHF_EXECINSTR;
425         if (AIsExec != BIsExec)
426           return BIsExec;
427 
428         // If we got here we know that both A and B and in the same PT_LOAD.
429         // The last requirement we have is to put nobits section last. The
430         // reason is that the only thing the dynamic linker will see about
431         // them is a p_memsz that is larger than p_filesz. Seeing that it
432         // zeros the end of the PT_LOAD, so that has to correspond to the
433         // nobits sections.
434         return A->getType() != SHT_NOBITS && B->getType() == SHT_NOBITS;
435       });
436 
437   // Always put StrTabSec last so that no section names are added to it after
438   // it's finalized.
439   OutputSections.push_back(&StrTabSec);
440 
441   for (unsigned I = 0, N = OutputSections.size(); I < N; ++I)
442     OutputSections[I]->setSectionIndex(I + 1);
443 
444   // Fill the DynStrSec early.
445   DynamicSec.finalize();
446 }
447 
448 template <class ELFT>
449 static bool needsPHDR(OutputSectionBase<ELFT::Is64Bits> *Sec) {
450   return Sec->getFlags() & SHF_ALLOC;
451 }
452 
453 // Visits all sections to assign incremental, non-overlapping RVAs and
454 // file offsets.
455 template <class ELFT> void Writer<ELFT>::assignAddresses() {
456   assert(!OutputSections.empty() && "No output sections to layout!");
457   uintX_t VA = getVAStart();
458   uintX_t FileOff = 0;
459 
460   FileOff += sizeof(Elf_Ehdr);
461   VA += sizeof(Elf_Ehdr);
462 
463   // Reserve space for PHDRs.
464   ProgramHeaderOff = FileOff;
465   FileOff = RoundUpToAlignment(FileOff, PageSize);
466   VA = RoundUpToAlignment(VA, PageSize);
467 
468   if (needsInterpSection())
469     PHDRs.push_back(&InterpPHDR);
470 
471   // Create a PHDR for the file header.
472   PHDRs.push_back(&FileHeaderPHDR);
473   FileHeaderPHDR.Header.p_vaddr = getVAStart();
474   FileHeaderPHDR.Header.p_paddr = getVAStart();
475   FileHeaderPHDR.Header.p_align = PageSize;
476 
477   for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
478     StrTabSec.add(Sec->getName());
479     Sec->finalize();
480 
481     if (Sec->getSize()) {
482       uintX_t Flags = toPHDRFlags(Sec->getFlags());
483       ProgramHeader<ELFT> *Last = PHDRs.back();
484       if (Last->Header.p_flags != Flags || !needsPHDR<ELFT>(Sec)) {
485         // Flags changed. End current PHDR and potentially create a new one.
486         if (!Last->Closed) {
487           Last->Header.p_filesz = FileOff - Last->Header.p_offset;
488           Last->Header.p_memsz = VA - Last->Header.p_vaddr;
489           Last->Closed = true;
490         }
491 
492         if (needsPHDR<ELFT>(Sec)) {
493           VA = RoundUpToAlignment(VA, PageSize);
494           FileOff = RoundUpToAlignment(FileOff, PageSize);
495           PHDRs.push_back(new (PAlloc)
496                               ProgramHeader<ELFT>(PT_LOAD, Flags, FileOff, VA));
497         }
498       }
499     }
500 
501     uintX_t Align = Sec->getAlign();
502     uintX_t Size = Sec->getSize();
503     if (Sec->getFlags() & SHF_ALLOC) {
504       VA = RoundUpToAlignment(VA, Align);
505       Sec->setVA(VA);
506       VA += Size;
507     }
508     FileOff = RoundUpToAlignment(FileOff, Align);
509     Sec->setFileOffset(FileOff);
510     if (Sec->getType() != SHT_NOBITS)
511       FileOff += Size;
512   }
513 
514   // Add a PHDR for the dynamic table.
515   if (needsDynamicSections())
516     PHDRs.push_back(&DynamicPHDR);
517 
518   FileOff += OffsetToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4);
519 
520   // Add space for section headers.
521   SectionHeaderOff = FileOff;
522   FileOff += getNumSections() * sizeof(Elf_Shdr);
523   FileSize = FileOff;
524 }
525 
526 template <class ELFT> void Writer<ELFT>::writeHeader() {
527   uint8_t *Buf = Buffer->getBufferStart();
528   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
529   EHdr->e_ident[EI_MAG0] = 0x7F;
530   EHdr->e_ident[EI_MAG1] = 0x45;
531   EHdr->e_ident[EI_MAG2] = 0x4C;
532   EHdr->e_ident[EI_MAG3] = 0x46;
533   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
534   EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little
535                                ? ELFDATA2LSB
536                                : ELFDATA2MSB;
537   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
538 
539   const SymbolTable &Symtab = SymTabSec.getSymTable();
540   auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab.getFirstELF());
541   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
542 
543   // FIXME: Generalize the segment construction similar to how we create
544   // output sections.
545 
546   EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC;
547   EHdr->e_machine = FirstObj.getEMachine();
548   EHdr->e_version = EV_CURRENT;
549   SymbolBody *Entry = Symtab.getEntrySym();
550   EHdr->e_entry =
551       Entry ? getSymVA(cast<ELFSymbolBody<ELFT>>(*Entry), BssSec) : 0;
552   EHdr->e_phoff = ProgramHeaderOff;
553   EHdr->e_shoff = SectionHeaderOff;
554   EHdr->e_ehsize = sizeof(Elf_Ehdr);
555   EHdr->e_phentsize = sizeof(Elf_Phdr);
556   EHdr->e_phnum = PHDRs.size();
557   EHdr->e_shentsize = sizeof(Elf_Shdr);
558   EHdr->e_shnum = getNumSections();
559   EHdr->e_shstrndx = StrTabSec.getSectionIndex();
560 
561   // If nothing was merged into the file header PT_LOAD, set the size correctly.
562   if (FileHeaderPHDR.Header.p_filesz == PageSize) {
563     uint64_t Size = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * PHDRs.size();
564     FileHeaderPHDR.Header.p_filesz = Size;
565     FileHeaderPHDR.Header.p_memsz = Size;
566   }
567 
568   if (needsInterpSection())
569     InterpPHDR.setValuesFromSection(InterpSec);
570   if (needsDynamicSections())
571     DynamicPHDR.setValuesFromSection(DynamicSec);
572 
573   auto PHdrs = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
574   for (ProgramHeader<ELFT> *PHDR : PHDRs)
575     *PHdrs++ = PHDR->Header;
576 
577   auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
578   // First entry is null.
579   ++SHdrs;
580   for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
581     Sec->setNameOffset(StrTabSec.getFileOff(Sec->getName()));
582     Sec->template writeHeaderTo<ELFT::TargetEndianness>(SHdrs++);
583   }
584 }
585 
586 template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
587   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
588       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
589   error(BufferOrErr, Twine("failed to open ") + Path);
590   Buffer = std::move(*BufferOrErr);
591 }
592 
593 // Write section contents to a mmap'ed file.
594 template <class ELFT> void Writer<ELFT>::writeSections() {
595   uint8_t *Buf = Buffer->getBufferStart();
596   for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections)
597     Sec->writeTo(Buf + Sec->getFileOff());
598 }
599