xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 45faf47e)
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/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Support/FileOutputBuffer.h"
19 #include "llvm/Support/StringSaver.h"
20 
21 using namespace llvm;
22 using namespace llvm::ELF;
23 using namespace llvm::object;
24 
25 using namespace lld;
26 using namespace lld::elf2;
27 
28 namespace {
29 // The writer writes a SymbolTable result to a file.
30 template <class ELFT> class Writer {
31 public:
32   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
33   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
34   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
35   typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
36   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
37   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
38   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
39   Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
40   void run();
41 
42 private:
43   void copyLocalSymbols();
44   void createSections();
45   template <bool isRela>
46   void scanRelocs(const InputSection<ELFT> &C,
47                   iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels);
48   void scanRelocs(const InputSection<ELFT> &C);
49   void assignAddresses();
50   void openFile(StringRef OutputPath);
51   void writeHeader();
52   void writeSections();
53   bool needsInterpSection() const {
54     return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
55   }
56   bool isOutputDynamic() const {
57     return !Symtab.getSharedFiles().empty() || Config->Shared;
58   }
59   uintX_t getVAStart() const { return Config->Shared ? 0 : Target->getVAStart(); }
60   uintX_t getEntryAddr() const;
61   int getPhdrsNum() const;
62 
63   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
64 
65   SpecificBumpPtrAllocator<OutputSection<ELFT>> SecAlloc;
66   SpecificBumpPtrAllocator<MergeOutputSection<ELFT>> MSecAlloc;
67   BumpPtrAllocator Alloc;
68   std::vector<OutputSectionBase<ELFT> *> OutputSections;
69   unsigned getNumSections() const { return OutputSections.size() + 1; }
70 
71   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
72   void setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags, uintX_t FileOff,
73                uintX_t VA, uintX_t Size, uintX_t Align);
74   void copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT> *From);
75 
76   SymbolTable<ELFT> &Symtab;
77   std::vector<Elf_Phdr> Phdrs;
78 
79   uintX_t FileSize;
80   uintX_t SectionHeaderOff;
81 };
82 } // anonymous namespace
83 
84 template <class ELFT> void lld::elf2::writeResult(SymbolTable<ELFT> *Symtab) {
85   // Initialize output sections that are handled by Writer specially.
86   // Don't reorder because the order of initialization matters.
87   InterpSection<ELFT> Interp;
88   Out<ELFT>::Interp = &Interp;
89   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
90   Out<ELFT>::ShStrTab = &ShStrTab;
91   StringTableSection<ELFT> StrTab(".strtab", false);
92   if (!Config->StripAll)
93     Out<ELFT>::StrTab = &StrTab;
94   StringTableSection<ELFT> DynStrTab(".dynstr", true);
95   Out<ELFT>::DynStrTab = &DynStrTab;
96   OutputSection<ELFT> Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
97   Out<ELFT>::Bss = &Bss;
98   GotSection<ELFT> Got;
99   Out<ELFT>::Got = &Got;
100   GotPltSection<ELFT> GotPlt;
101   if (Target->supportsLazyRelocations())
102     Out<ELFT>::GotPlt = &GotPlt;
103   PltSection<ELFT> Plt;
104   Out<ELFT>::Plt = &Plt;
105   std::unique_ptr<SymbolTableSection<ELFT>> SymTab;
106   if (!Config->StripAll) {
107     SymTab.reset(new SymbolTableSection<ELFT>(*Symtab, *Out<ELFT>::StrTab));
108     Out<ELFT>::SymTab = SymTab.get();
109   }
110   SymbolTableSection<ELFT> DynSymTab(*Symtab, *Out<ELFT>::DynStrTab);
111   Out<ELFT>::DynSymTab = &DynSymTab;
112   HashTableSection<ELFT> HashTab;
113   if (Config->SysvHash)
114     Out<ELFT>::HashTab = &HashTab;
115   GnuHashTableSection<ELFT> GnuHashTab;
116   if (Config->GnuHash)
117     Out<ELFT>::GnuHashTab = &GnuHashTab;
118   bool IsRela = Symtab->shouldUseRela();
119   RelocationSection<ELFT> RelaDyn(IsRela ? ".rela.dyn" : ".rel.dyn", IsRela);
120   Out<ELFT>::RelaDyn = &RelaDyn;
121   RelocationSection<ELFT> RelaPlt(IsRela ? ".rela.plt" : ".rel.plt", IsRela);
122   if (Target->supportsLazyRelocations())
123     Out<ELFT>::RelaPlt = &RelaPlt;
124   DynamicSection<ELFT> Dynamic(*Symtab);
125   Out<ELFT>::Dynamic = &Dynamic;
126 
127   Writer<ELFT>(*Symtab).run();
128 }
129 
130 // The main function of the writer.
131 template <class ELFT> void Writer<ELFT>::run() {
132   if (!Config->DiscardAll)
133     copyLocalSymbols();
134   createSections();
135   assignAddresses();
136   openFile(Config->OutputFile);
137   writeHeader();
138   writeSections();
139   error(Buffer->commit());
140 }
141 
142 namespace {
143 template <bool Is64Bits> struct SectionKey {
144   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
145   StringRef Name;
146   uint32_t Type;
147   uintX_t Flags;
148   uintX_t EntSize;
149 };
150 }
151 namespace llvm {
152 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
153   static SectionKey<Is64Bits> getEmptyKey() {
154     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0,
155                                 0};
156   }
157   static SectionKey<Is64Bits> getTombstoneKey() {
158     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
159                                 0, 0};
160   }
161   static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
162     return hash_combine(Val.Name, Val.Type, Val.Flags, Val.EntSize);
163   }
164   static bool isEqual(const SectionKey<Is64Bits> &LHS,
165                       const SectionKey<Is64Bits> &RHS) {
166     return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
167            LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
168            LHS.EntSize == RHS.EntSize;
169   }
170 };
171 }
172 
173 // The reason we have to do this early scan is as follows
174 // * To mmap the output file, we need to know the size
175 // * For that, we need to know how many dynamic relocs we will have.
176 // It might be possible to avoid this by outputting the file with write:
177 // * Write the allocated output sections, computing addresses.
178 // * Apply relocations, recording which ones require a dynamic reloc.
179 // * Write the dynamic relocations.
180 // * Write the rest of the file.
181 template <class ELFT>
182 template <bool isRela>
183 void Writer<ELFT>::scanRelocs(
184     const InputSection<ELFT> &C,
185     iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
186   typedef Elf_Rel_Impl<ELFT, isRela> RelType;
187   const ObjectFile<ELFT> &File = *C.getFile();
188   for (const RelType &RI : Rels) {
189     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
190     SymbolBody *Body = File.getSymbolBody(SymIndex);
191     uint32_t Type = RI.getType(Config->Mips64EL);
192 
193     // Set "used" bit for --as-needed.
194     if (Body && Body->isUndefined() && !Body->isWeak())
195       if (auto *S = dyn_cast<SharedSymbol<ELFT>>(Body->repl()))
196         S->File->IsUsed = true;
197 
198     if (Body)
199       Body = Body->repl();
200     bool NeedsGot = false;
201     bool NeedsPlt = false;
202     if (Body) {
203       if (auto *E = dyn_cast<SharedSymbol<ELFT>>(Body)) {
204         if (E->NeedsCopy)
205           continue;
206         E->NeedsCopy = Target->relocNeedsCopy(Type, *Body);
207       }
208       NeedsPlt = Target->relocNeedsPlt(Type, *Body);
209       if (NeedsPlt) {
210         if (Body->isInPlt())
211           continue;
212         Out<ELFT>::Plt->addEntry(Body);
213       }
214       NeedsGot = Target->relocNeedsGot(Type, *Body);
215       if (NeedsGot) {
216         if (NeedsPlt && Target->supportsLazyRelocations()) {
217           Out<ELFT>::GotPlt->addEntry(Body);
218         } else {
219           if (Body->isInGot())
220             continue;
221           Out<ELFT>::Got->addEntry(Body);
222         }
223       }
224     }
225 
226     bool CBP = canBePreempted(Body, NeedsGot);
227     if (!CBP && (!Config->Shared || Target->isRelRelative(Type)))
228       continue;
229     if (CBP)
230       Body->setUsedInDynamicReloc();
231     if (NeedsPlt && Target->supportsLazyRelocations())
232       Out<ELFT>::RelaPlt->addReloc({C, RI});
233     else
234       Out<ELFT>::RelaDyn->addReloc({C, RI});
235   }
236 }
237 
238 template <class ELFT>
239 void Writer<ELFT>::scanRelocs(const InputSection<ELFT> &C) {
240   ObjectFile<ELFT> *File = C.getFile();
241   ELFFile<ELFT> &EObj = File->getObj();
242 
243   if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
244     return;
245 
246   for (const Elf_Shdr *RelSec : C.RelocSections) {
247     if (RelSec->sh_type == SHT_RELA)
248       scanRelocs(C, EObj.relas(RelSec));
249     else
250       scanRelocs(C, EObj.rels(RelSec));
251   }
252 }
253 
254 template <class ELFT>
255 static void reportUndefined(const SymbolTable<ELFT> &S, const SymbolBody &Sym) {
256   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
257   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
258 
259   if (Config->Shared && !Config->NoUndefined)
260     return;
261 
262   const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym;
263   ELFFileBase<ELFT> *SymFile = nullptr;
264 
265   for (const std::unique_ptr<ObjectFile<ELFT>> &File : S.getObjectFiles()) {
266     Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
267     if (&SymE > Syms.begin() && &SymE < Syms.end())
268       SymFile = File.get();
269   }
270 
271   std::string Message = "undefined symbol: " + Sym.getName().str();
272   if (SymFile)
273     Message += " in " + SymFile->getName().str();
274   if (Config->NoInhibitExec)
275     warning(Message);
276   else
277     error(Message);
278 }
279 
280 // Local symbols are not in the linker's symbol table. This function scans
281 // each object file's symbol table to copy local symbols to the output.
282 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
283   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
284     for (const Elf_Sym &Sym : F->getLocalSymbols()) {
285       ErrorOr<StringRef> SymNameOrErr = Sym.getName(F->getStringTable());
286       error(SymNameOrErr);
287       StringRef SymName = *SymNameOrErr;
288       if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym))
289         continue;
290       if (Out<ELFT>::SymTab)
291         Out<ELFT>::SymTab->addLocalSymbol(SymName);
292     }
293   }
294 }
295 
296 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
297 // we would like to make sure appear is a specific order to maximize their
298 // coverage by a single signed 16-bit offset from the TOC base pointer.
299 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
300 // sections. This will put it next to the loaded special PPC64 sections (and,
301 // thus, within reach of the TOC base pointer).
302 static int getPPC64SectionRank(StringRef SectionName) {
303   return StringSwitch<int>(SectionName)
304            .Case(".tocbss", 0)
305            .Case(".branch_lt", 2)
306            .Case(".toc", 3)
307            .Case(".toc1", 4)
308            .Case(".opd", 5)
309            .Default(1);
310 }
311 
312 // Output section ordering is determined by this function.
313 template <class ELFT>
314 static bool compareOutputSections(OutputSectionBase<ELFT> *A,
315                                   OutputSectionBase<ELFT> *B) {
316   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
317 
318   uintX_t AFlags = A->getFlags();
319   uintX_t BFlags = B->getFlags();
320 
321   // Allocatable sections go first to reduce the total PT_LOAD size and
322   // so debug info doesn't change addresses in actual code.
323   bool AIsAlloc = AFlags & SHF_ALLOC;
324   bool BIsAlloc = BFlags & SHF_ALLOC;
325   if (AIsAlloc != BIsAlloc)
326     return AIsAlloc;
327 
328   // We don't have any special requirements for the relative order of
329   // two non allocatable sections.
330   if (!AIsAlloc)
331     return false;
332 
333   // We want the read only sections first so that they go in the PT_LOAD
334   // covering the program headers at the start of the file.
335   bool AIsWritable = AFlags & SHF_WRITE;
336   bool BIsWritable = BFlags & SHF_WRITE;
337   if (AIsWritable != BIsWritable)
338     return BIsWritable;
339 
340   // For a corresponding reason, put non exec sections first (the program
341   // header PT_LOAD is not executable).
342   bool AIsExec = AFlags & SHF_EXECINSTR;
343   bool BIsExec = BFlags & SHF_EXECINSTR;
344   if (AIsExec != BIsExec)
345     return BIsExec;
346 
347   // If we got here we know that both A and B are in the same PT_LOAD.
348 
349   // The TLS initialization block needs to be a single contiguous block in a R/W
350   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
351   // sections are placed here as they don't take up virtual address space in the
352   // PT_LOAD.
353   bool AIsTLS = AFlags & SHF_TLS;
354   bool BIsTLS = BFlags & SHF_TLS;
355   if (AIsTLS != BIsTLS)
356     return AIsTLS;
357 
358   // The next requirement we have is to put nobits sections last. The
359   // reason is that the only thing the dynamic linker will see about
360   // them is a p_memsz that is larger than p_filesz. Seeing that it
361   // zeros the end of the PT_LOAD, so that has to correspond to the
362   // nobits sections.
363   bool AIsNoBits = A->getType() == SHT_NOBITS;
364   bool BIsNoBits = B->getType() == SHT_NOBITS;
365   if (AIsNoBits != BIsNoBits)
366     return BIsNoBits;
367 
368   // Some architectures have additional ordering restrictions for sections
369   // within the same PT_LOAD.
370   if (Config->EMachine == EM_PPC64)
371     return getPPC64SectionRank(A->getName()) <
372            getPPC64SectionRank(B->getName());
373 
374   return false;
375 }
376 
377 // Until this function is called, common symbols do not belong to any section.
378 // This function adds them to end of BSS section.
379 template <class ELFT>
380 static void addCommonSymbols(std::vector<DefinedCommon<ELFT> *> &Syms) {
381   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
382   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
383 
384   // Sort the common symbols by alignment as an heuristic to pack them better.
385   std::stable_sort(
386     Syms.begin(), Syms.end(),
387     [](const DefinedCommon<ELFT> *A, const DefinedCommon<ELFT> *B) {
388       return A->MaxAlignment > B->MaxAlignment;
389     });
390 
391   uintX_t Off = Out<ELFT>::Bss->getSize();
392   for (DefinedCommon<ELFT> *C : Syms) {
393     const Elf_Sym &Sym = C->Sym;
394     uintX_t Align = C->MaxAlignment;
395     Off = RoundUpToAlignment(Off, Align);
396     C->OffsetInBSS = Off;
397     Off += Sym.st_size;
398   }
399 
400   Out<ELFT>::Bss->setSize(Off);
401 }
402 
403 template <class ELFT>
404 static void addSharedCopySymbols(std::vector<SharedSymbol<ELFT> *> &Syms) {
405   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
406   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
407 
408   uintX_t Off = Out<ELFT>::Bss->getSize();
409   for (SharedSymbol<ELFT> *C : Syms) {
410     const Elf_Sym &Sym = C->Sym;
411     // We don't know the exact alignment requirement for the data copied by a
412     // copy relocation, so align that to 16 byte boundaries that should be large
413     // enough unconditionally.
414     Off = RoundUpToAlignment(Off, 16);
415     C->OffsetInBSS = Off;
416     Off += Sym.st_size;
417   }
418   Out<ELFT>::Bss->setSize(Off);
419 }
420 
421 static StringRef getOutputName(StringRef S) {
422   if (S.startswith(".text."))
423     return ".text";
424   if (S.startswith(".rodata."))
425     return ".rodata";
426   if (S.startswith(".data."))
427     return ".data";
428   if (S.startswith(".bss."))
429     return ".bss";
430   return S;
431 }
432 
433 // Create output section objects and add them to OutputSections.
434 template <class ELFT> void Writer<ELFT>::createSections() {
435   // .interp needs to be on the first page in the output file.
436   if (needsInterpSection())
437     OutputSections.push_back(Out<ELFT>::Interp);
438 
439   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map;
440 
441   OutputSections.push_back(Out<ELFT>::Bss);
442   Map[{Out<ELFT>::Bss->getName(), Out<ELFT>::Bss->getType(),
443        Out<ELFT>::Bss->getFlags(), 0}] = Out<ELFT>::Bss;
444 
445   std::vector<OutputSectionBase<ELFT> *> RegularSections;
446 
447   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
448     for (InputSectionBase<ELFT> *C : F->getSections()) {
449       if (!C || !C->isLive() || C == &InputSection<ELFT>::Discarded)
450         continue;
451       const Elf_Shdr *H = C->getSectionHdr();
452       uintX_t OutFlags = H->sh_flags & ~SHF_GROUP;
453       // For SHF_MERGE we create different output sections for each sh_entsize.
454       // This makes each output section simple and keeps a single level
455       // mapping from input to output.
456       auto *IS = dyn_cast<InputSection<ELFT>>(C);
457       uintX_t EntSize = IS ? 0 : H->sh_entsize;
458       SectionKey<ELFT::Is64Bits> Key{getOutputName(C->getSectionName()),
459                                      H->sh_type, OutFlags, EntSize};
460       OutputSectionBase<ELFT> *&Sec = Map[Key];
461       if (!Sec) {
462         if (IS)
463           Sec = new (SecAlloc.Allocate())
464               OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
465         else
466           Sec = new (MSecAlloc.Allocate())
467               MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
468         OutputSections.push_back(Sec);
469         RegularSections.push_back(Sec);
470       }
471       if (IS)
472         static_cast<OutputSection<ELFT> *>(Sec)->addSection(IS);
473       else
474         static_cast<MergeOutputSection<ELFT> *>(Sec)
475             ->addSection(cast<MergeInputSection<ELFT>>(C));
476     }
477   }
478 
479   Out<ELFT>::Dynamic->PreInitArraySec = Map.lookup(
480       {".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC, 0});
481   Out<ELFT>::Dynamic->InitArraySec =
482       Map.lookup({".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC, 0});
483   Out<ELFT>::Dynamic->FiniArraySec =
484       Map.lookup({".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC, 0});
485 
486   auto AddStartEnd = [&](StringRef Start, StringRef End,
487                          OutputSectionBase<ELFT> *OS) {
488     if (OS) {
489       Symtab.addSyntheticSym(Start, *OS, 0);
490       Symtab.addSyntheticSym(End, *OS, OS->getSize());
491     } else {
492       Symtab.addIgnoredSym(Start);
493       Symtab.addIgnoredSym(End);
494     }
495   };
496 
497   AddStartEnd("__preinit_array_start", "__preinit_array_end",
498               Out<ELFT>::Dynamic->PreInitArraySec);
499   AddStartEnd("__init_array_start", "__init_array_end",
500               Out<ELFT>::Dynamic->InitArraySec);
501   AddStartEnd("__fini_array_start", "__fini_array_end",
502               Out<ELFT>::Dynamic->FiniArraySec);
503 
504   for (OutputSectionBase<ELFT> *Sec : RegularSections)
505     addStartStopSymbols(Sec);
506 
507   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
508   // static linking the linker is required to optimize away any references to
509   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
510   // to avoid the undefined symbol error.
511   if (!isOutputDynamic())
512     Symtab.addIgnoredSym("__tls_get_addr");
513 
514   // Scan relocations. This must be done after every symbol is declared so that
515   // we can correctly decide if a dynamic relocation is needed.
516   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles())
517     for (InputSectionBase<ELFT> *B : F->getSections())
518       if (auto *S = dyn_cast_or_null<InputSection<ELFT>>(B))
519         if (S != &InputSection<ELFT>::Discarded)
520           if (S->isLive())
521             scanRelocs(*S);
522 
523   std::vector<DefinedCommon<ELFT> *> CommonSymbols;
524   std::vector<SharedSymbol<ELFT> *> SharedCopySymbols;
525   for (auto &P : Symtab.getSymbols()) {
526     SymbolBody *Body = P.second->Body;
527     if (auto *U = dyn_cast<Undefined<ELFT>>(Body))
528       if (!U->isWeak() && !U->canKeepUndefined())
529         reportUndefined<ELFT>(Symtab, *Body);
530 
531     if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
532       CommonSymbols.push_back(C);
533     if (auto *SC = dyn_cast<SharedSymbol<ELFT>>(Body))
534       if (SC->NeedsCopy)
535         SharedCopySymbols.push_back(SC);
536 
537     if (!includeInSymtab<ELFT>(*Body))
538       continue;
539     if (Out<ELFT>::SymTab)
540       Out<ELFT>::SymTab->addSymbol(Body);
541 
542     if (isOutputDynamic() && includeInDynamicSymtab(*Body))
543       Out<ELFT>::DynSymTab->addSymbol(Body);
544   }
545   addCommonSymbols(CommonSymbols);
546   addSharedCopySymbols(SharedCopySymbols);
547 
548   // This order is not the same as the final output order
549   // because we sort the sections using their attributes below.
550   if (Out<ELFT>::SymTab)
551     OutputSections.push_back(Out<ELFT>::SymTab);
552   OutputSections.push_back(Out<ELFT>::ShStrTab);
553   if (Out<ELFT>::StrTab)
554     OutputSections.push_back(Out<ELFT>::StrTab);
555   if (isOutputDynamic()) {
556     OutputSections.push_back(Out<ELFT>::DynSymTab);
557     if (Out<ELFT>::GnuHashTab)
558       OutputSections.push_back(Out<ELFT>::GnuHashTab);
559     if (Out<ELFT>::HashTab)
560       OutputSections.push_back(Out<ELFT>::HashTab);
561     OutputSections.push_back(Out<ELFT>::Dynamic);
562     OutputSections.push_back(Out<ELFT>::DynStrTab);
563     if (Out<ELFT>::RelaDyn->hasRelocs())
564       OutputSections.push_back(Out<ELFT>::RelaDyn);
565     if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs())
566       OutputSections.push_back(Out<ELFT>::RelaPlt);
567   }
568   if (!Out<ELFT>::Got->empty())
569     OutputSections.push_back(Out<ELFT>::Got);
570   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
571     OutputSections.push_back(Out<ELFT>::GotPlt);
572   if (!Out<ELFT>::Plt->empty())
573     OutputSections.push_back(Out<ELFT>::Plt);
574 
575   std::stable_sort(OutputSections.begin(), OutputSections.end(),
576                    compareOutputSections<ELFT>);
577 
578   for (unsigned I = 0, N = OutputSections.size(); I < N; ++I)
579     OutputSections[I]->SectionIndex = I + 1;
580 
581   for (OutputSectionBase<ELFT> *Sec : OutputSections)
582     Out<ELFT>::ShStrTab->add(Sec->getName());
583 
584   // Fill the DynStrTab early because Dynamic adds strings to
585   // DynStrTab but .dynstr may appear before .dynamic.
586   Out<ELFT>::Dynamic->finalize();
587 
588   // Fill other section headers.
589   for (OutputSectionBase<ELFT> *Sec : OutputSections)
590     Sec->finalize();
591 
592   // If we have a .opd section (used under PPC64 for function descriptors),
593   // store a pointer to it here so that we can use it later when processing
594   // relocations.
595   Out<ELFT>::Opd = Map.lookup({".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC, 0});
596 }
597 
598 static bool isAlpha(char C) {
599   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
600 }
601 
602 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
603 
604 // Returns true if S is valid as a C language identifier.
605 static bool isValidCIdentifier(StringRef S) {
606   if (S.empty() || !isAlpha(S[0]))
607     return false;
608   return std::all_of(S.begin() + 1, S.end(), isAlnum);
609 }
610 
611 // If a section name is valid as a C identifier (which is rare because of
612 // the leading '.'), linkers are expected to define __start_<secname> and
613 // __stop_<secname> symbols. They are at beginning and end of the section,
614 // respectively. This is not requested by the ELF standard, but GNU ld and
615 // gold provide the feature, and used by many programs.
616 template <class ELFT>
617 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
618   StringRef S = Sec->getName();
619   if (!isValidCIdentifier(S))
620     return;
621   StringSaver Saver(Alloc);
622   StringRef Start = Saver.save("__start_" + S);
623   StringRef Stop = Saver.save("__stop_" + S);
624   if (Symtab.isUndefined(Start))
625     Symtab.addSyntheticSym(Start, *Sec, 0);
626   if (Symtab.isUndefined(Stop))
627     Symtab.addSyntheticSym(Stop, *Sec, Sec->getSize());
628 }
629 
630 template <class ELFT> static bool needsPhdr(OutputSectionBase<ELFT> *Sec) {
631   return Sec->getFlags() & SHF_ALLOC;
632 }
633 
634 static uint32_t toPhdrFlags(uint64_t Flags) {
635   uint32_t Ret = PF_R;
636   if (Flags & SHF_WRITE)
637     Ret |= PF_W;
638   if (Flags & SHF_EXECINSTR)
639     Ret |= PF_X;
640   return Ret;
641 }
642 
643 template <class ELFT>
644 static bool consumesVirtualAddressSpace(OutputSectionBase<ELFT> *Sec) {
645   return (Sec->getFlags() & SHF_ALLOC) &&
646          // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
647          // responsible for allocating space for them, not the PT_LOAD that
648          // contains the TLS initialization image.
649          !((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS);
650 }
651 
652 // Visits all sections to create PHDRs and to assign incremental,
653 // non-overlapping addresses to output sections.
654 template <class ELFT> void Writer<ELFT>::assignAddresses() {
655   uintX_t VA = getVAStart() + sizeof(Elf_Ehdr);
656   uintX_t FileOff = sizeof(Elf_Ehdr);
657 
658   // Calculate and reserve the space for the program header first so that
659   // the first section can start right after the program header.
660   Phdrs.resize(getPhdrsNum());
661   size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size();
662 
663   // The first phdr entry is PT_PHDR which describes the program header itself.
664   setPhdr(&Phdrs[0], PT_PHDR, PF_R, FileOff, VA, PhdrSize, /*Align=*/8);
665   FileOff += PhdrSize;
666   VA += PhdrSize;
667 
668   // PT_INTERP must be the second entry if exists.
669   int PhdrIdx = 0;
670   Elf_Phdr *Interp = nullptr;
671   if (needsInterpSection())
672     Interp = &Phdrs[++PhdrIdx];
673 
674   // Add the first PT_LOAD segment for regular output sections.
675   setPhdr(&Phdrs[++PhdrIdx], PT_LOAD, PF_R, 0, getVAStart(), FileOff,
676           Target->getPageSize());
677 
678   // Create phdrs as we assign VAs and file offsets to all output sections.
679   SmallPtrSet<Elf_Phdr *, 8> Closed;
680   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
681     if (Sec->getSize()) {
682       uintX_t Flags = toPhdrFlags(Sec->getFlags());
683       Elf_Phdr *Last = &Phdrs[PhdrIdx];
684       if (Last->p_flags != Flags || !needsPhdr<ELFT>(Sec)) {
685         // Flags changed. End current Phdr and potentially create a new one.
686         if (Closed.insert(Last).second) {
687           Last->p_filesz = FileOff - Last->p_offset;
688           Last->p_memsz = VA - Last->p_vaddr;
689         }
690 
691         if (needsPhdr<ELFT>(Sec)) {
692           VA = RoundUpToAlignment(VA, Target->getPageSize());
693           FileOff = RoundUpToAlignment(FileOff, Target->getPageSize());
694           Elf_Phdr *PH = &Phdrs[++PhdrIdx];
695           setPhdr(PH, PT_LOAD, Flags, FileOff, VA, 0, Target->getPageSize());
696         }
697       }
698     }
699 
700     if (consumesVirtualAddressSpace<ELFT>(Sec)) {
701       VA = RoundUpToAlignment(VA, Sec->getAlign());
702       Sec->setVA(VA);
703       VA += Sec->getSize();
704     }
705     FileOff = RoundUpToAlignment(FileOff, Sec->getAlign());
706     Sec->setFileOffset(FileOff);
707     if (Sec->getType() != SHT_NOBITS)
708       FileOff += Sec->getSize();
709   }
710 
711   // Add an entry for .dynamic.
712   if (isOutputDynamic()) {
713     Elf_Phdr *PH = &Phdrs[++PhdrIdx];
714     PH->p_type = PT_DYNAMIC;
715     copyPhdr(PH, Out<ELFT>::Dynamic);
716   }
717 
718   // Fix up PT_INTERP as we now know the address of .interp section.
719   if (Interp) {
720     Interp->p_type = PT_INTERP;
721     copyPhdr(Interp, Out<ELFT>::Interp);
722   }
723 
724   // Add space for section headers.
725   SectionHeaderOff = RoundUpToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4);
726   FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr);
727 }
728 
729 // Returns the number of PHDR entries.
730 template <class ELFT> int Writer<ELFT>::getPhdrsNum() const {
731   int I = 2; // 2 for PT_PHDR and the first PT_LOAD
732   if (needsInterpSection())
733     ++I;
734   if (isOutputDynamic())
735     ++I;
736   uintX_t Last = PF_R;
737   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
738     if (!Sec->getSize() || !needsPhdr<ELFT>(Sec))
739       continue;
740     uintX_t Flags = toPhdrFlags(Sec->getFlags());
741     if (Last != Flags) {
742       Last = Flags;
743       ++I;
744     }
745   }
746   return I;
747 }
748 
749 template <class ELFT> void Writer<ELFT>::writeHeader() {
750   uint8_t *Buf = Buffer->getBufferStart();
751   memcpy(Buf, "\177ELF", 4);
752 
753   // Write the ELF header.
754   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
755   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
756   EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little
757                                ? ELFDATA2LSB
758                                : ELFDATA2MSB;
759   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
760 
761   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
762   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
763 
764   EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC;
765   EHdr->e_machine = FirstObj.getEMachine();
766   EHdr->e_version = EV_CURRENT;
767   EHdr->e_entry = getEntryAddr();
768   EHdr->e_phoff = sizeof(Elf_Ehdr);
769   EHdr->e_shoff = SectionHeaderOff;
770   EHdr->e_ehsize = sizeof(Elf_Ehdr);
771   EHdr->e_phentsize = sizeof(Elf_Phdr);
772   EHdr->e_phnum = Phdrs.size();
773   EHdr->e_shentsize = sizeof(Elf_Shdr);
774   EHdr->e_shnum = getNumSections();
775   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
776 
777   // Write the program header table.
778   memcpy(Buf + EHdr->e_phoff, &Phdrs[0], Phdrs.size() * sizeof(Phdrs[0]));
779 
780   // Write the section header table. Note that the first table entry is null.
781   auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
782   for (OutputSectionBase<ELFT> *Sec : OutputSections)
783     Sec->writeHeaderTo(++SHdrs);
784 }
785 
786 template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
787   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
788       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
789   error(BufferOrErr, Twine("failed to open ") + Path);
790   Buffer = std::move(*BufferOrErr);
791 }
792 
793 // Write section contents to a mmap'ed file.
794 template <class ELFT> void Writer<ELFT>::writeSections() {
795   uint8_t *Buf = Buffer->getBufferStart();
796 
797   // PPC64 needs to process relocations in the .opd section before processing
798   // relocations in code-containing sections.
799   if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) {
800     Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
801     Sec->writeTo(Buf + Sec->getFileOff());
802   }
803 
804   for (OutputSectionBase<ELFT> *Sec : OutputSections)
805     if (Sec != Out<ELFT>::Opd)
806       Sec->writeTo(Buf + Sec->getFileOff());
807 }
808 
809 template <class ELFT>
810 typename ELFFile<ELFT>::uintX_t Writer<ELFT>::getEntryAddr() const {
811   if (Config->EntrySym) {
812     if (auto *E = dyn_cast<ELFSymbolBody<ELFT>>(Config->EntrySym->repl()))
813       return getSymVA<ELFT>(*E);
814     return 0;
815   }
816   if (Config->EntryAddr != uint64_t(-1))
817     return Config->EntryAddr;
818   return 0;
819 }
820 
821 template <class ELFT>
822 void Writer<ELFT>::setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags,
823                            uintX_t FileOff, uintX_t VA, uintX_t Size,
824                            uintX_t Align) {
825   PH->p_type = Type;
826   PH->p_flags = Flags;
827   PH->p_offset = FileOff;
828   PH->p_vaddr = VA;
829   PH->p_paddr = VA;
830   PH->p_filesz = Size;
831   PH->p_memsz = Size;
832   PH->p_align = Align;
833 }
834 
835 template <class ELFT>
836 void Writer<ELFT>::copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT> *From) {
837   PH->p_flags = toPhdrFlags(From->getFlags());
838   PH->p_offset = From->getFileOff();
839   PH->p_vaddr = From->getVA();
840   PH->p_paddr = From->getVA();
841   PH->p_filesz = From->getSize();
842   PH->p_memsz = From->getSize();
843   PH->p_align = From->getAlign();
844 }
845 
846 template void lld::elf2::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
847 template void lld::elf2::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
848 template void lld::elf2::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
849 template void lld::elf2::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
850