1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The ELF component of yaml2obj.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/StringTableBuilder.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/ObjectYAML/DWARFEmitter.h"
22 #include "llvm/ObjectYAML/DWARFYAML.h"
23 #include "llvm/ObjectYAML/ELFYAML.h"
24 #include "llvm/ObjectYAML/yaml2obj.h"
25 #include "llvm/Support/EndianStream.h"
26 #include "llvm/Support/Errc.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/YAMLTraits.h"
32 #include "llvm/Support/raw_ostream.h"
33 
34 using namespace llvm;
35 
36 // This class is used to build up a contiguous binary blob while keeping
37 // track of an offset in the output (which notionally begins at
38 // `InitialOffset`).
39 // The blob might be limited to an arbitrary size. All attempts to write data
40 // are ignored and the error condition is remembered once the limit is reached.
41 // Such an approach allows us to simplify the code by delaying error reporting
42 // and doing it at a convenient time.
43 namespace {
44 class ContiguousBlobAccumulator {
45   const uint64_t InitialOffset;
46   const uint64_t MaxSize;
47 
48   SmallVector<char, 128> Buf;
49   raw_svector_ostream OS;
50   Error ReachedLimitErr = Error::success();
51 
52   bool checkLimit(uint64_t Size) {
53     if (!ReachedLimitErr && getOffset() + Size <= MaxSize)
54       return true;
55     if (!ReachedLimitErr)
56       ReachedLimitErr = createStringError(errc::invalid_argument,
57                                           "reached the output size limit");
58     return false;
59   }
60 
61 public:
62   ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
63       : InitialOffset(BaseOffset), MaxSize(SizeLimit), OS(Buf) {}
64 
65   uint64_t tell() const { return OS.tell(); }
66   uint64_t getOffset() const { return InitialOffset + OS.tell(); }
67   void writeBlobToStream(raw_ostream &Out) const { Out << OS.str(); }
68 
69   Error takeLimitError() {
70     // Request to write 0 bytes to check we did not reach the limit.
71     checkLimit(0);
72     return std::move(ReachedLimitErr);
73   }
74 
75   /// \returns The new offset.
76   uint64_t padToAlignment(unsigned Align) {
77     uint64_t CurrentOffset = getOffset();
78     if (ReachedLimitErr)
79       return CurrentOffset;
80 
81     uint64_t AlignedOffset = alignTo(CurrentOffset, Align == 0 ? 1 : Align);
82     uint64_t PaddingSize = AlignedOffset - CurrentOffset;
83     if (!checkLimit(PaddingSize))
84       return CurrentOffset;
85 
86     writeZeros(PaddingSize);
87     return AlignedOffset;
88   }
89 
90   raw_ostream *getRawOS(uint64_t Size) {
91     if (checkLimit(Size))
92       return &OS;
93     return nullptr;
94   }
95 
96   void writeAsBinary(const yaml::BinaryRef &Bin, uint64_t N = UINT64_MAX) {
97     if (!checkLimit(Bin.binary_size()))
98       return;
99     Bin.writeAsBinary(OS, N);
100   }
101 
102   void writeZeros(uint64_t Num) {
103     if (checkLimit(Num))
104       OS.write_zeros(Num);
105   }
106 
107   void write(const char *Ptr, size_t Size) {
108     if (checkLimit(Size))
109       OS.write(Ptr, Size);
110   }
111 
112   void write(unsigned char C) {
113     if (checkLimit(1))
114       OS.write(C);
115   }
116 
117   unsigned writeULEB128(uint64_t Val) {
118     if (!checkLimit(sizeof(uint64_t)))
119       return 0;
120     return encodeULEB128(Val, OS);
121   }
122 
123   template <typename T> void write(T Val, support::endianness E) {
124     if (checkLimit(sizeof(T)))
125       support::endian::write<T>(OS, Val, E);
126   }
127 
128   void updateDataAt(uint64_t Pos, void *Data, size_t Size) {
129     assert(Pos >= InitialOffset && Pos + Size <= getOffset());
130     memcpy(&Buf[Pos - InitialOffset], Data, Size);
131   }
132 };
133 
134 // Used to keep track of section and symbol names, so that in the YAML file
135 // sections and symbols can be referenced by name instead of by index.
136 class NameToIdxMap {
137   StringMap<unsigned> Map;
138 
139 public:
140   /// \Returns false if name is already present in the map.
141   bool addName(StringRef Name, unsigned Ndx) {
142     return Map.insert({Name, Ndx}).second;
143   }
144   /// \Returns false if name is not present in the map.
145   bool lookup(StringRef Name, unsigned &Idx) const {
146     auto I = Map.find(Name);
147     if (I == Map.end())
148       return false;
149     Idx = I->getValue();
150     return true;
151   }
152   /// Asserts if name is not present in the map.
153   unsigned get(StringRef Name) const {
154     unsigned Idx;
155     if (lookup(Name, Idx))
156       return Idx;
157     assert(false && "Expected section not found in index");
158     return 0;
159   }
160   unsigned size() const { return Map.size(); }
161 };
162 
163 namespace {
164 struct Fragment {
165   uint64_t Offset;
166   uint64_t Size;
167   uint32_t Type;
168   uint64_t AddrAlign;
169 };
170 } // namespace
171 
172 /// "Single point of truth" for the ELF file construction.
173 /// TODO: This class still has a ways to go before it is truly a "single
174 /// point of truth".
175 template <class ELFT> class ELFState {
176   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
177 
178   enum class SymtabType { Static, Dynamic };
179 
180   /// The future symbol table string section.
181   StringTableBuilder DotStrtab{StringTableBuilder::ELF};
182 
183   /// The future section header string table section, if a unique string table
184   /// is needed. Don't reference this variable direectly: use the
185   /// ShStrtabStrings member instead.
186   StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
187 
188   /// The future dynamic symbol string section.
189   StringTableBuilder DotDynstr{StringTableBuilder::ELF};
190 
191   /// The name of the section header string table section. If it is .strtab or
192   /// .dynstr, the section header strings will be written to the same string
193   /// table as the static/dynamic symbols respectively. Otherwise a dedicated
194   /// section will be created with that name.
195   StringRef SectionHeaderStringTableName = ".shstrtab";
196   StringTableBuilder *ShStrtabStrings = &DotShStrtab;
197 
198   NameToIdxMap SN2I;
199   NameToIdxMap SymN2I;
200   NameToIdxMap DynSymN2I;
201   ELFYAML::Object &Doc;
202 
203   StringSet<> ExcludedSectionHeaders;
204 
205   uint64_t LocationCounter = 0;
206   bool HasError = false;
207   yaml::ErrorHandler ErrHandler;
208   void reportError(const Twine &Msg);
209   void reportError(Error Err);
210 
211   std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
212                                     const StringTableBuilder &Strtab);
213   unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
214   unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
215 
216   void buildSectionIndex();
217   void buildSymbolIndexes();
218   void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
219   bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
220                           StringRef SecName, ELFYAML::Section *YAMLSec);
221   void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
222                           ContiguousBlobAccumulator &CBA);
223   void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
224                                ContiguousBlobAccumulator &CBA,
225                                ELFYAML::Section *YAMLSec);
226   void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
227                                StringTableBuilder &STB,
228                                ContiguousBlobAccumulator &CBA,
229                                ELFYAML::Section *YAMLSec);
230   void initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
231                               ContiguousBlobAccumulator &CBA,
232                               ELFYAML::Section *YAMLSec);
233   void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
234                               std::vector<Elf_Shdr> &SHeaders);
235 
236   std::vector<Fragment>
237   getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
238                    ArrayRef<typename ELFT::Shdr> SHeaders);
239 
240   void finalizeStrings();
241   void writeELFHeader(raw_ostream &OS);
242   void writeSectionContent(Elf_Shdr &SHeader,
243                            const ELFYAML::NoBitsSection &Section,
244                            ContiguousBlobAccumulator &CBA);
245   void writeSectionContent(Elf_Shdr &SHeader,
246                            const ELFYAML::RawContentSection &Section,
247                            ContiguousBlobAccumulator &CBA);
248   void writeSectionContent(Elf_Shdr &SHeader,
249                            const ELFYAML::RelocationSection &Section,
250                            ContiguousBlobAccumulator &CBA);
251   void writeSectionContent(Elf_Shdr &SHeader,
252                            const ELFYAML::RelrSection &Section,
253                            ContiguousBlobAccumulator &CBA);
254   void writeSectionContent(Elf_Shdr &SHeader,
255                            const ELFYAML::GroupSection &Group,
256                            ContiguousBlobAccumulator &CBA);
257   void writeSectionContent(Elf_Shdr &SHeader,
258                            const ELFYAML::SymtabShndxSection &Shndx,
259                            ContiguousBlobAccumulator &CBA);
260   void writeSectionContent(Elf_Shdr &SHeader,
261                            const ELFYAML::SymverSection &Section,
262                            ContiguousBlobAccumulator &CBA);
263   void writeSectionContent(Elf_Shdr &SHeader,
264                            const ELFYAML::VerneedSection &Section,
265                            ContiguousBlobAccumulator &CBA);
266   void writeSectionContent(Elf_Shdr &SHeader,
267                            const ELFYAML::VerdefSection &Section,
268                            ContiguousBlobAccumulator &CBA);
269   void writeSectionContent(Elf_Shdr &SHeader,
270                            const ELFYAML::ARMIndexTableSection &Section,
271                            ContiguousBlobAccumulator &CBA);
272   void writeSectionContent(Elf_Shdr &SHeader,
273                            const ELFYAML::MipsABIFlags &Section,
274                            ContiguousBlobAccumulator &CBA);
275   void writeSectionContent(Elf_Shdr &SHeader,
276                            const ELFYAML::DynamicSection &Section,
277                            ContiguousBlobAccumulator &CBA);
278   void writeSectionContent(Elf_Shdr &SHeader,
279                            const ELFYAML::StackSizesSection &Section,
280                            ContiguousBlobAccumulator &CBA);
281   void writeSectionContent(Elf_Shdr &SHeader,
282                            const ELFYAML::BBAddrMapSection &Section,
283                            ContiguousBlobAccumulator &CBA);
284   void writeSectionContent(Elf_Shdr &SHeader,
285                            const ELFYAML::HashSection &Section,
286                            ContiguousBlobAccumulator &CBA);
287   void writeSectionContent(Elf_Shdr &SHeader,
288                            const ELFYAML::AddrsigSection &Section,
289                            ContiguousBlobAccumulator &CBA);
290   void writeSectionContent(Elf_Shdr &SHeader,
291                            const ELFYAML::NoteSection &Section,
292                            ContiguousBlobAccumulator &CBA);
293   void writeSectionContent(Elf_Shdr &SHeader,
294                            const ELFYAML::GnuHashSection &Section,
295                            ContiguousBlobAccumulator &CBA);
296   void writeSectionContent(Elf_Shdr &SHeader,
297                            const ELFYAML::LinkerOptionsSection &Section,
298                            ContiguousBlobAccumulator &CBA);
299   void writeSectionContent(Elf_Shdr &SHeader,
300                            const ELFYAML::DependentLibrariesSection &Section,
301                            ContiguousBlobAccumulator &CBA);
302   void writeSectionContent(Elf_Shdr &SHeader,
303                            const ELFYAML::CallGraphProfileSection &Section,
304                            ContiguousBlobAccumulator &CBA);
305 
306   void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
307 
308   ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
309 
310   void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec);
311 
312   DenseMap<StringRef, size_t> buildSectionHeaderReorderMap();
313 
314   BumpPtrAllocator StringAlloc;
315   uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
316                          llvm::Optional<llvm::yaml::Hex64> Offset);
317 
318   uint64_t getSectionNameOffset(StringRef Name);
319 
320 public:
321   static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
322                        yaml::ErrorHandler EH, uint64_t MaxSize);
323 };
324 } // end anonymous namespace
325 
326 template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
327   return A.size() * sizeof(T);
328 }
329 
330 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
331   OS.write((const char *)A.data(), arrayDataSize(A));
332 }
333 
334 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
335 
336 template <class ELFT>
337 ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
338     : Doc(D), ErrHandler(EH) {
339   // The input may explicitly request to store the section header table strings
340   // in the same string table as dynamic or static symbol names. Set the
341   // ShStrtabStrings member accordingly.
342   if (Doc.Header.SectionHeaderStringTable) {
343     SectionHeaderStringTableName = *Doc.Header.SectionHeaderStringTable;
344     if (*Doc.Header.SectionHeaderStringTable == ".strtab")
345       ShStrtabStrings = &DotStrtab;
346     else if (*Doc.Header.SectionHeaderStringTable == ".dynstr")
347       ShStrtabStrings = &DotDynstr;
348     // Otherwise, the unique table will be used.
349   }
350 
351   std::vector<ELFYAML::Section *> Sections = Doc.getSections();
352   // Insert SHT_NULL section implicitly when it is not defined in YAML.
353   if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL)
354     Doc.Chunks.insert(
355         Doc.Chunks.begin(),
356         std::make_unique<ELFYAML::Section>(
357             ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true));
358 
359   StringSet<> DocSections;
360   ELFYAML::SectionHeaderTable *SecHdrTable = nullptr;
361   for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
362     const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
363 
364     // We might have an explicit section header table declaration.
365     if (auto S = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
366       if (SecHdrTable)
367         reportError("multiple section header tables are not allowed");
368       SecHdrTable = S;
369       continue;
370     }
371 
372     // We add a technical suffix for each unnamed section/fill. It does not
373     // affect the output, but allows us to map them by name in the code and
374     // report better error messages.
375     if (C->Name.empty()) {
376       std::string NewName = ELFYAML::appendUniqueSuffix(
377           /*Name=*/"", "index " + Twine(I));
378       C->Name = StringRef(NewName).copy(StringAlloc);
379       assert(ELFYAML::dropUniqueSuffix(C->Name).empty());
380     }
381 
382     if (!DocSections.insert(C->Name).second)
383       reportError("repeated section/fill name: '" + C->Name +
384                   "' at YAML section/fill number " + Twine(I));
385   }
386 
387   SmallSetVector<StringRef, 8> ImplicitSections;
388   if (Doc.DynamicSymbols) {
389     if (SectionHeaderStringTableName == ".dynsym")
390       reportError("cannot use '.dynsym' as the section header name table when "
391                   "there are dynamic symbols");
392     ImplicitSections.insert(".dynsym");
393     ImplicitSections.insert(".dynstr");
394   }
395   if (Doc.Symbols) {
396     if (SectionHeaderStringTableName == ".symtab")
397       reportError("cannot use '.symtab' as the section header name table when "
398                   "there are symbols");
399     ImplicitSections.insert(".symtab");
400   }
401   if (Doc.DWARF)
402     for (StringRef DebugSecName : Doc.DWARF->getNonEmptySectionNames()) {
403       std::string SecName = ("." + DebugSecName).str();
404       // TODO: For .debug_str it should be possible to share the string table,
405       // in the same manner as the symbol string tables.
406       if (SectionHeaderStringTableName == SecName)
407         reportError("cannot use '" + SecName +
408                     "' as the section header name table when it is needed for "
409                     "DWARF output");
410       ImplicitSections.insert(StringRef(SecName).copy(StringAlloc));
411     }
412   // TODO: Only create the .strtab here if any symbols have been requested.
413   ImplicitSections.insert(".strtab");
414   if (!SecHdrTable || !SecHdrTable->NoHeaders.getValueOr(false))
415     ImplicitSections.insert(SectionHeaderStringTableName);
416 
417   // Insert placeholders for implicit sections that are not
418   // defined explicitly in YAML.
419   for (StringRef SecName : ImplicitSections) {
420     if (DocSections.count(SecName))
421       continue;
422 
423     std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
424         ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/);
425     Sec->Name = SecName;
426 
427     if (SecName == SectionHeaderStringTableName)
428       Sec->Type = ELF::SHT_STRTAB;
429     else if (SecName == ".dynsym")
430       Sec->Type = ELF::SHT_DYNSYM;
431     else if (SecName == ".symtab")
432       Sec->Type = ELF::SHT_SYMTAB;
433     else
434       Sec->Type = ELF::SHT_STRTAB;
435 
436     // When the section header table is explicitly defined at the end of the
437     // sections list, it is reasonable to assume that the user wants to reorder
438     // section headers, but still wants to place the section header table after
439     // all sections, like it normally happens. In this case we want to insert
440     // other implicit sections right before the section header table.
441     if (Doc.Chunks.back().get() == SecHdrTable)
442       Doc.Chunks.insert(Doc.Chunks.end() - 1, std::move(Sec));
443     else
444       Doc.Chunks.push_back(std::move(Sec));
445   }
446 
447   // Insert the section header table implicitly at the end, when it is not
448   // explicitly defined.
449   if (!SecHdrTable)
450     Doc.Chunks.push_back(
451         std::make_unique<ELFYAML::SectionHeaderTable>(/*IsImplicit=*/true));
452 }
453 
454 template <class ELFT>
455 void ELFState<ELFT>::writeELFHeader(raw_ostream &OS) {
456   using namespace llvm::ELF;
457 
458   Elf_Ehdr Header;
459   zero(Header);
460   Header.e_ident[EI_MAG0] = 0x7f;
461   Header.e_ident[EI_MAG1] = 'E';
462   Header.e_ident[EI_MAG2] = 'L';
463   Header.e_ident[EI_MAG3] = 'F';
464   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
465   Header.e_ident[EI_DATA] = Doc.Header.Data;
466   Header.e_ident[EI_VERSION] = EV_CURRENT;
467   Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
468   Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
469   Header.e_type = Doc.Header.Type;
470 
471   if (Doc.Header.Machine)
472     Header.e_machine = *Doc.Header.Machine;
473   else
474     Header.e_machine = EM_NONE;
475 
476   Header.e_version = EV_CURRENT;
477   Header.e_entry = Doc.Header.Entry;
478   Header.e_flags = Doc.Header.Flags;
479   Header.e_ehsize = sizeof(Elf_Ehdr);
480 
481   if (Doc.Header.EPhOff)
482     Header.e_phoff = *Doc.Header.EPhOff;
483   else if (!Doc.ProgramHeaders.empty())
484     Header.e_phoff = sizeof(Header);
485   else
486     Header.e_phoff = 0;
487 
488   if (Doc.Header.EPhEntSize)
489     Header.e_phentsize = *Doc.Header.EPhEntSize;
490   else if (!Doc.ProgramHeaders.empty())
491     Header.e_phentsize = sizeof(Elf_Phdr);
492   else
493     Header.e_phentsize = 0;
494 
495   if (Doc.Header.EPhNum)
496     Header.e_phnum = *Doc.Header.EPhNum;
497   else if (!Doc.ProgramHeaders.empty())
498     Header.e_phnum = Doc.ProgramHeaders.size();
499   else
500     Header.e_phnum = 0;
501 
502   Header.e_shentsize = Doc.Header.EShEntSize ? (uint16_t)*Doc.Header.EShEntSize
503                                              : sizeof(Elf_Shdr);
504 
505   const ELFYAML::SectionHeaderTable &SectionHeaders =
506       Doc.getSectionHeaderTable();
507 
508   if (Doc.Header.EShOff)
509     Header.e_shoff = *Doc.Header.EShOff;
510   else if (SectionHeaders.Offset)
511     Header.e_shoff = *SectionHeaders.Offset;
512   else
513     Header.e_shoff = 0;
514 
515   if (Doc.Header.EShNum)
516     Header.e_shnum = *Doc.Header.EShNum;
517   else
518     Header.e_shnum = SectionHeaders.getNumHeaders(Doc.getSections().size());
519 
520   if (Doc.Header.EShStrNdx)
521     Header.e_shstrndx = *Doc.Header.EShStrNdx;
522   else if (SectionHeaders.Offset &&
523            !ExcludedSectionHeaders.count(SectionHeaderStringTableName))
524     Header.e_shstrndx = SN2I.get(SectionHeaderStringTableName);
525   else
526     Header.e_shstrndx = 0;
527 
528   OS.write((const char *)&Header, sizeof(Header));
529 }
530 
531 template <class ELFT>
532 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
533   DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
534   DenseMap<StringRef, size_t> NameToIndex;
535   for (size_t I = 0, E = Doc.Chunks.size(); I != E; ++I) {
536     if (auto S = dyn_cast<ELFYAML::Fill>(Doc.Chunks[I].get()))
537       NameToFill[S->Name] = S;
538     NameToIndex[Doc.Chunks[I]->Name] = I + 1;
539   }
540 
541   std::vector<ELFYAML::Section *> Sections = Doc.getSections();
542   for (size_t I = 0, E = Doc.ProgramHeaders.size(); I != E; ++I) {
543     ELFYAML::ProgramHeader &YamlPhdr = Doc.ProgramHeaders[I];
544     Elf_Phdr Phdr;
545     zero(Phdr);
546     Phdr.p_type = YamlPhdr.Type;
547     Phdr.p_flags = YamlPhdr.Flags;
548     Phdr.p_vaddr = YamlPhdr.VAddr;
549     Phdr.p_paddr = YamlPhdr.PAddr;
550     PHeaders.push_back(Phdr);
551 
552     if (!YamlPhdr.FirstSec && !YamlPhdr.LastSec)
553       continue;
554 
555     // Get the index of the section, or 0 in the case when the section doesn't exist.
556     size_t First = NameToIndex[*YamlPhdr.FirstSec];
557     if (!First)
558       reportError("unknown section or fill referenced: '" + *YamlPhdr.FirstSec +
559                   "' by the 'FirstSec' key of the program header with index " +
560                   Twine(I));
561     size_t Last = NameToIndex[*YamlPhdr.LastSec];
562     if (!Last)
563       reportError("unknown section or fill referenced: '" + *YamlPhdr.LastSec +
564                   "' by the 'LastSec' key of the program header with index " +
565                   Twine(I));
566     if (!First || !Last)
567       continue;
568 
569     if (First > Last)
570       reportError("program header with index " + Twine(I) +
571                   ": the section index of " + *YamlPhdr.FirstSec +
572                   " is greater than the index of " + *YamlPhdr.LastSec);
573 
574     for (size_t I = First; I <= Last; ++I)
575       YamlPhdr.Chunks.push_back(Doc.Chunks[I - 1].get());
576   }
577 }
578 
579 template <class ELFT>
580 unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
581                                         StringRef LocSym) {
582   assert(LocSec.empty() || LocSym.empty());
583 
584   unsigned Index;
585   if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
586     if (!LocSym.empty())
587       reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
588                   LocSym + "'");
589     else
590       reportError("unknown section referenced: '" + S + "' by YAML section '" +
591                   LocSec + "'");
592     return 0;
593   }
594 
595   const ELFYAML::SectionHeaderTable &SectionHeaders =
596       Doc.getSectionHeaderTable();
597   if (SectionHeaders.IsImplicit ||
598       (SectionHeaders.NoHeaders && !SectionHeaders.NoHeaders.getValue()) ||
599       SectionHeaders.isDefault())
600     return Index;
601 
602   assert(!SectionHeaders.NoHeaders.getValueOr(false) ||
603          !SectionHeaders.Sections);
604   size_t FirstExcluded =
605       SectionHeaders.Sections ? SectionHeaders.Sections->size() : 0;
606   if (Index > FirstExcluded) {
607     if (LocSym.empty())
608       reportError("unable to link '" + LocSec + "' to excluded section '" + S +
609                   "'");
610     else
611       reportError("excluded section referenced: '" + S + "'  by symbol '" +
612                   LocSym + "'");
613   }
614   return Index;
615 }
616 
617 template <class ELFT>
618 unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
619                                        bool IsDynamic) {
620   const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
621   unsigned Index;
622   // Here we try to look up S in the symbol table. If it is not there,
623   // treat its value as a symbol index.
624   if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
625     reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
626                 LocSec + "'");
627     return 0;
628   }
629   return Index;
630 }
631 
632 template <class ELFT>
633 static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) {
634   if (!From)
635     return;
636   if (From->ShAddrAlign)
637     To.sh_addralign = *From->ShAddrAlign;
638   if (From->ShFlags)
639     To.sh_flags = *From->ShFlags;
640   if (From->ShName)
641     To.sh_name = *From->ShName;
642   if (From->ShOffset)
643     To.sh_offset = *From->ShOffset;
644   if (From->ShSize)
645     To.sh_size = *From->ShSize;
646   if (From->ShType)
647     To.sh_type = *From->ShType;
648 }
649 
650 template <class ELFT>
651 bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
652                                         Elf_Shdr &Header, StringRef SecName,
653                                         ELFYAML::Section *YAMLSec) {
654   // Check if the header was already initialized.
655   if (Header.sh_offset)
656     return false;
657 
658   if (SecName == ".strtab")
659     initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
660   else if (SecName == ".dynstr")
661     initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
662   else if (SecName == SectionHeaderStringTableName)
663     initStrtabSectionHeader(Header, SecName, *ShStrtabStrings, CBA, YAMLSec);
664   else if (SecName == ".symtab")
665     initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
666   else if (SecName == ".dynsym")
667     initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
668   else if (SecName.startswith(".debug_")) {
669     // If a ".debug_*" section's type is a preserved one, e.g., SHT_DYNAMIC, we
670     // will not treat it as a debug section.
671     if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
672       return false;
673     initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
674   } else
675     return false;
676 
677   LocationCounter += Header.sh_size;
678 
679   // Override section fields if requested.
680   overrideFields<ELFT>(YAMLSec, Header);
681   return true;
682 }
683 
684 constexpr char SuffixStart = '(';
685 constexpr char SuffixEnd = ')';
686 
687 std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name,
688                                               const Twine &Msg) {
689   // Do not add a space when a Name is empty.
690   std::string Ret = Name.empty() ? "" : Name.str() + ' ';
691   return Ret + (Twine(SuffixStart) + Msg + Twine(SuffixEnd)).str();
692 }
693 
694 StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
695   if (S.empty() || S.back() != SuffixEnd)
696     return S;
697 
698   // A special case for empty names. See appendUniqueSuffix() above.
699   size_t SuffixPos = S.rfind(SuffixStart);
700   if (SuffixPos == 0)
701     return "";
702 
703   if (SuffixPos == StringRef::npos || S[SuffixPos - 1] != ' ')
704     return S;
705   return S.substr(0, SuffixPos - 1);
706 }
707 
708 template <class ELFT>
709 uint64_t ELFState<ELFT>::getSectionNameOffset(StringRef Name) {
710   // If a section is excluded from section headers, we do not save its name in
711   // the string table.
712   if (ExcludedSectionHeaders.count(Name))
713     return 0;
714   return ShStrtabStrings->getOffset(Name);
715 }
716 
717 static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
718                              const Optional<yaml::BinaryRef> &Content,
719                              const Optional<llvm::yaml::Hex64> &Size) {
720   size_t ContentSize = 0;
721   if (Content) {
722     CBA.writeAsBinary(*Content);
723     ContentSize = Content->binary_size();
724   }
725 
726   if (!Size)
727     return ContentSize;
728 
729   CBA.writeZeros(*Size - ContentSize);
730   return *Size;
731 }
732 
733 static StringRef getDefaultLinkSec(unsigned SecType) {
734   switch (SecType) {
735   case ELF::SHT_REL:
736   case ELF::SHT_RELA:
737   case ELF::SHT_GROUP:
738   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
739   case ELF::SHT_LLVM_ADDRSIG:
740     return ".symtab";
741   case ELF::SHT_GNU_versym:
742   case ELF::SHT_HASH:
743   case ELF::SHT_GNU_HASH:
744     return ".dynsym";
745   case ELF::SHT_DYNSYM:
746   case ELF::SHT_GNU_verdef:
747   case ELF::SHT_GNU_verneed:
748     return ".dynstr";
749   case ELF::SHT_SYMTAB:
750     return ".strtab";
751   default:
752     return "";
753   }
754 }
755 
756 template <class ELFT>
757 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
758                                         ContiguousBlobAccumulator &CBA) {
759   // Ensure SHN_UNDEF entry is present. An all-zero section header is a
760   // valid SHN_UNDEF entry since SHT_NULL == 0.
761   SHeaders.resize(Doc.getSections().size());
762 
763   for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
764     if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) {
765       S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
766       writeFill(*S, CBA);
767       LocationCounter += S->Size;
768       continue;
769     }
770 
771     if (ELFYAML::SectionHeaderTable *S =
772             dyn_cast<ELFYAML::SectionHeaderTable>(D.get())) {
773       if (S->NoHeaders.getValueOr(false))
774         continue;
775 
776       if (!S->Offset)
777         S->Offset = alignToOffset(CBA, sizeof(typename ELFT::uint),
778                                   /*Offset=*/None);
779       else
780         S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
781 
782       uint64_t Size = S->getNumHeaders(SHeaders.size()) * sizeof(Elf_Shdr);
783       // The full section header information might be not available here, so
784       // fill the space with zeroes as a placeholder.
785       CBA.writeZeros(Size);
786       LocationCounter += Size;
787       continue;
788     }
789 
790     ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get());
791     bool IsFirstUndefSection = Sec == Doc.getSections().front();
792     if (IsFirstUndefSection && Sec->IsImplicit)
793       continue;
794 
795     Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
796     if (Sec->Link) {
797       SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
798     } else {
799       StringRef LinkSec = getDefaultLinkSec(Sec->Type);
800       unsigned Link = 0;
801       if (!LinkSec.empty() && !ExcludedSectionHeaders.count(LinkSec) &&
802           SN2I.lookup(LinkSec, Link))
803         SHeader.sh_link = Link;
804     }
805 
806     if (Sec->EntSize)
807       SHeader.sh_entsize = *Sec->EntSize;
808     else
809       SHeader.sh_entsize = ELFYAML::getDefaultShEntSize<ELFT>(
810           Doc.Header.Machine.getValueOr(ELF::EM_NONE), Sec->Type, Sec->Name);
811 
812     // We have a few sections like string or symbol tables that are usually
813     // added implicitly to the end. However, if they are explicitly specified
814     // in the YAML, we need to write them here. This ensures the file offset
815     // remains correct.
816     if (initImplicitHeader(CBA, SHeader, Sec->Name,
817                            Sec->IsImplicit ? nullptr : Sec))
818       continue;
819 
820     assert(Sec && "It can't be null unless it is an implicit section. But all "
821                   "implicit sections should already have been handled above.");
822 
823     SHeader.sh_name =
824         getSectionNameOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
825     SHeader.sh_type = Sec->Type;
826     if (Sec->Flags)
827       SHeader.sh_flags = *Sec->Flags;
828     SHeader.sh_addralign = Sec->AddressAlign;
829 
830     // Set the offset for all sections, except the SHN_UNDEF section with index
831     // 0 when not explicitly requested.
832     if (!IsFirstUndefSection || Sec->Offset)
833       SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset);
834 
835     assignSectionAddress(SHeader, Sec);
836 
837     if (IsFirstUndefSection) {
838       if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
839         // We do not write any content for special SHN_UNDEF section.
840         if (RawSec->Size)
841           SHeader.sh_size = *RawSec->Size;
842         if (RawSec->Info)
843           SHeader.sh_info = *RawSec->Info;
844       }
845 
846       LocationCounter += SHeader.sh_size;
847       overrideFields<ELFT>(Sec, SHeader);
848       continue;
849     }
850 
851     if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->Content || Sec->Size))
852       SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size);
853 
854     if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
855       writeSectionContent(SHeader, *S, CBA);
856     } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
857       writeSectionContent(SHeader, *S, CBA);
858     } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
859       writeSectionContent(SHeader, *S, CBA);
860     } else if (auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
861       writeSectionContent(SHeader, *S, CBA);
862     } else if (auto S = dyn_cast<ELFYAML::GroupSection>(Sec)) {
863       writeSectionContent(SHeader, *S, CBA);
864     } else if (auto S = dyn_cast<ELFYAML::ARMIndexTableSection>(Sec)) {
865       writeSectionContent(SHeader, *S, CBA);
866     } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
867       writeSectionContent(SHeader, *S, CBA);
868     } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
869       writeSectionContent(SHeader, *S, CBA);
870     } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
871       writeSectionContent(SHeader, *S, CBA);
872     } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
873       writeSectionContent(SHeader, *S, CBA);
874     } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
875       writeSectionContent(SHeader, *S, CBA);
876     } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
877       writeSectionContent(SHeader, *S, CBA);
878     } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
879       writeSectionContent(SHeader, *S, CBA);
880     } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
881       writeSectionContent(SHeader, *S, CBA);
882     } else if (auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
883       writeSectionContent(SHeader, *S, CBA);
884     } else if (auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
885       writeSectionContent(SHeader, *S, CBA);
886     } else if (auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
887       writeSectionContent(SHeader, *S, CBA);
888     } else if (auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
889       writeSectionContent(SHeader, *S, CBA);
890     } else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
891       writeSectionContent(SHeader, *S, CBA);
892     } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
893       writeSectionContent(SHeader, *S, CBA);
894     } else if (auto S = dyn_cast<ELFYAML::BBAddrMapSection>(Sec)) {
895       writeSectionContent(SHeader, *S, CBA);
896     } else {
897       llvm_unreachable("Unknown section type");
898     }
899 
900     LocationCounter += SHeader.sh_size;
901 
902     // Override section fields if requested.
903     overrideFields<ELFT>(Sec, SHeader);
904   }
905 }
906 
907 template <class ELFT>
908 void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
909                                           ELFYAML::Section *YAMLSec) {
910   if (YAMLSec && YAMLSec->Address) {
911     SHeader.sh_addr = *YAMLSec->Address;
912     LocationCounter = *YAMLSec->Address;
913     return;
914   }
915 
916   // sh_addr represents the address in the memory image of a process. Sections
917   // in a relocatable object file or non-allocatable sections do not need
918   // sh_addr assignment.
919   if (Doc.Header.Type.value == ELF::ET_REL ||
920       !(SHeader.sh_flags & ELF::SHF_ALLOC))
921     return;
922 
923   LocationCounter =
924       alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
925   SHeader.sh_addr = LocationCounter;
926 }
927 
928 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
929   for (size_t I = 0; I < Symbols.size(); ++I)
930     if (Symbols[I].Binding.value != ELF::STB_LOCAL)
931       return I;
932   return Symbols.size();
933 }
934 
935 template <class ELFT>
936 std::vector<typename ELFT::Sym>
937 ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
938                              const StringTableBuilder &Strtab) {
939   std::vector<Elf_Sym> Ret;
940   Ret.resize(Symbols.size() + 1);
941 
942   size_t I = 0;
943   for (const ELFYAML::Symbol &Sym : Symbols) {
944     Elf_Sym &Symbol = Ret[++I];
945 
946     // If NameIndex, which contains the name offset, is explicitly specified, we
947     // use it. This is useful for preparing broken objects. Otherwise, we add
948     // the specified Name to the string table builder to get its offset.
949     if (Sym.StName)
950       Symbol.st_name = *Sym.StName;
951     else if (!Sym.Name.empty())
952       Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
953 
954     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
955     if (Sym.Section)
956       Symbol.st_shndx = toSectionIndex(*Sym.Section, "", Sym.Name);
957     else if (Sym.Index)
958       Symbol.st_shndx = *Sym.Index;
959 
960     Symbol.st_value = Sym.Value.getValueOr(yaml::Hex64(0));
961     Symbol.st_other = Sym.Other ? *Sym.Other : 0;
962     Symbol.st_size = Sym.Size.getValueOr(yaml::Hex64(0));
963   }
964 
965   return Ret;
966 }
967 
968 template <class ELFT>
969 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
970                                              SymtabType STType,
971                                              ContiguousBlobAccumulator &CBA,
972                                              ELFYAML::Section *YAMLSec) {
973 
974   bool IsStatic = STType == SymtabType::Static;
975   ArrayRef<ELFYAML::Symbol> Symbols;
976   if (IsStatic && Doc.Symbols)
977     Symbols = *Doc.Symbols;
978   else if (!IsStatic && Doc.DynamicSymbols)
979     Symbols = *Doc.DynamicSymbols;
980 
981   ELFYAML::RawContentSection *RawSec =
982       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
983   if (RawSec && (RawSec->Content || RawSec->Size)) {
984     bool HasSymbolsDescription =
985         (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
986     if (HasSymbolsDescription) {
987       StringRef Property = (IsStatic ? "`Symbols`" : "`DynamicSymbols`");
988       if (RawSec->Content)
989         reportError("cannot specify both `Content` and " + Property +
990                     " for symbol table section '" + RawSec->Name + "'");
991       if (RawSec->Size)
992         reportError("cannot specify both `Size` and " + Property +
993                     " for symbol table section '" + RawSec->Name + "'");
994       return;
995     }
996   }
997 
998   SHeader.sh_name = getSectionNameOffset(IsStatic ? ".symtab" : ".dynsym");
999 
1000   if (YAMLSec)
1001     SHeader.sh_type = YAMLSec->Type;
1002   else
1003     SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
1004 
1005   if (YAMLSec && YAMLSec->Flags)
1006     SHeader.sh_flags = *YAMLSec->Flags;
1007   else if (!IsStatic)
1008     SHeader.sh_flags = ELF::SHF_ALLOC;
1009 
1010   // If the symbol table section is explicitly described in the YAML
1011   // then we should set the fields requested.
1012   SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
1013                                              : findFirstNonGlobal(Symbols) + 1;
1014   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
1015 
1016   assignSectionAddress(SHeader, YAMLSec);
1017 
1018   SHeader.sh_offset =
1019       alignToOffset(CBA, SHeader.sh_addralign, RawSec ? RawSec->Offset : None);
1020 
1021   if (RawSec && (RawSec->Content || RawSec->Size)) {
1022     assert(Symbols.empty());
1023     SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1024     return;
1025   }
1026 
1027   std::vector<Elf_Sym> Syms =
1028       toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
1029   SHeader.sh_size = Syms.size() * sizeof(Elf_Sym);
1030   CBA.write((const char *)Syms.data(), SHeader.sh_size);
1031 }
1032 
1033 template <class ELFT>
1034 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1035                                              StringTableBuilder &STB,
1036                                              ContiguousBlobAccumulator &CBA,
1037                                              ELFYAML::Section *YAMLSec) {
1038   SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
1039   SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
1040   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
1041 
1042   ELFYAML::RawContentSection *RawSec =
1043       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1044 
1045   SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1046                                     YAMLSec ? YAMLSec->Offset : None);
1047 
1048   if (RawSec && (RawSec->Content || RawSec->Size)) {
1049     SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1050   } else {
1051     if (raw_ostream *OS = CBA.getRawOS(STB.getSize()))
1052       STB.write(*OS);
1053     SHeader.sh_size = STB.getSize();
1054   }
1055 
1056   if (RawSec && RawSec->Info)
1057     SHeader.sh_info = *RawSec->Info;
1058 
1059   if (YAMLSec && YAMLSec->Flags)
1060     SHeader.sh_flags = *YAMLSec->Flags;
1061   else if (Name == ".dynstr")
1062     SHeader.sh_flags = ELF::SHF_ALLOC;
1063 
1064   assignSectionAddress(SHeader, YAMLSec);
1065 }
1066 
1067 static bool shouldEmitDWARF(DWARFYAML::Data &DWARF, StringRef Name) {
1068   SetVector<StringRef> DebugSecNames = DWARF.getNonEmptySectionNames();
1069   return Name.consume_front(".") && DebugSecNames.count(Name);
1070 }
1071 
1072 template <class ELFT>
1073 Expected<uint64_t> emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
1074                              const DWARFYAML::Data &DWARF,
1075                              ContiguousBlobAccumulator &CBA) {
1076   // We are unable to predict the size of debug data, so we request to write 0
1077   // bytes. This should always return us an output stream unless CBA is already
1078   // in an error state.
1079   raw_ostream *OS = CBA.getRawOS(0);
1080   if (!OS)
1081     return 0;
1082 
1083   uint64_t BeginOffset = CBA.tell();
1084 
1085   auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Name.substr(1));
1086   if (Error Err = EmitFunc(*OS, DWARF))
1087     return std::move(Err);
1088 
1089   return CBA.tell() - BeginOffset;
1090 }
1091 
1092 template <class ELFT>
1093 void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1094                                             ContiguousBlobAccumulator &CBA,
1095                                             ELFYAML::Section *YAMLSec) {
1096   SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
1097   SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
1098   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
1099   SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1100                                     YAMLSec ? YAMLSec->Offset : None);
1101 
1102   ELFYAML::RawContentSection *RawSec =
1103       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1104   if (Doc.DWARF && shouldEmitDWARF(*Doc.DWARF, Name)) {
1105     if (RawSec && (RawSec->Content || RawSec->Size))
1106       reportError("cannot specify section '" + Name +
1107                   "' contents in the 'DWARF' entry and the 'Content' "
1108                   "or 'Size' in the 'Sections' entry at the same time");
1109     else {
1110       if (Expected<uint64_t> ShSizeOrErr =
1111               emitDWARF<ELFT>(SHeader, Name, *Doc.DWARF, CBA))
1112         SHeader.sh_size = *ShSizeOrErr;
1113       else
1114         reportError(ShSizeOrErr.takeError());
1115     }
1116   } else if (RawSec)
1117     SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1118   else
1119     llvm_unreachable("debug sections can only be initialized via the 'DWARF' "
1120                      "entry or a RawContentSection");
1121 
1122   if (RawSec && RawSec->Info)
1123     SHeader.sh_info = *RawSec->Info;
1124 
1125   if (YAMLSec && YAMLSec->Flags)
1126     SHeader.sh_flags = *YAMLSec->Flags;
1127   else if (Name == ".debug_str")
1128     SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
1129 
1130   assignSectionAddress(SHeader, YAMLSec);
1131 }
1132 
1133 template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
1134   ErrHandler(Msg);
1135   HasError = true;
1136 }
1137 
1138 template <class ELFT> void ELFState<ELFT>::reportError(Error Err) {
1139   handleAllErrors(std::move(Err), [&](const ErrorInfoBase &Err) {
1140     reportError(Err.message());
1141   });
1142 }
1143 
1144 template <class ELFT>
1145 std::vector<Fragment>
1146 ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
1147                                  ArrayRef<Elf_Shdr> SHeaders) {
1148   std::vector<Fragment> Ret;
1149   for (const ELFYAML::Chunk *C : Phdr.Chunks) {
1150     if (const ELFYAML::Fill *F = dyn_cast<ELFYAML::Fill>(C)) {
1151       Ret.push_back({*F->Offset, F->Size, llvm::ELF::SHT_PROGBITS,
1152                      /*ShAddrAlign=*/1});
1153       continue;
1154     }
1155 
1156     const ELFYAML::Section *S = cast<ELFYAML::Section>(C);
1157     const Elf_Shdr &H = SHeaders[SN2I.get(S->Name)];
1158     Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
1159   }
1160   return Ret;
1161 }
1162 
1163 template <class ELFT>
1164 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1165                                             std::vector<Elf_Shdr> &SHeaders) {
1166   uint32_t PhdrIdx = 0;
1167   for (auto &YamlPhdr : Doc.ProgramHeaders) {
1168     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1169     std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1170     if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
1171           return A.Offset < B.Offset;
1172         }))
1173       reportError("sections in the program header with index " +
1174                   Twine(PhdrIdx) + " are not sorted by their file offset");
1175 
1176     if (YamlPhdr.Offset) {
1177       if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset)
1178         reportError("'Offset' for segment with index " + Twine(PhdrIdx) +
1179                     " must be less than or equal to the minimum file offset of "
1180                     "all included sections (0x" +
1181                     Twine::utohexstr(Fragments.front().Offset) + ")");
1182       PHeader.p_offset = *YamlPhdr.Offset;
1183     } else if (!Fragments.empty()) {
1184       PHeader.p_offset = Fragments.front().Offset;
1185     }
1186 
1187     // Set the file size if not set explicitly.
1188     if (YamlPhdr.FileSize) {
1189       PHeader.p_filesz = *YamlPhdr.FileSize;
1190     } else if (!Fragments.empty()) {
1191       uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1192       // SHT_NOBITS sections occupy no physical space in a file, we should not
1193       // take their sizes into account when calculating the file size of a
1194       // segment.
1195       if (Fragments.back().Type != llvm::ELF::SHT_NOBITS)
1196         FileSize += Fragments.back().Size;
1197       PHeader.p_filesz = FileSize;
1198     }
1199 
1200     // Find the maximum offset of the end of a section in order to set p_memsz.
1201     uint64_t MemOffset = PHeader.p_offset;
1202     for (const Fragment &F : Fragments)
1203       MemOffset = std::max(MemOffset, F.Offset + F.Size);
1204     // Set the memory size if not set explicitly.
1205     PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
1206                                        : MemOffset - PHeader.p_offset;
1207 
1208     if (YamlPhdr.Align) {
1209       PHeader.p_align = *YamlPhdr.Align;
1210     } else {
1211       // Set the alignment of the segment to be the maximum alignment of the
1212       // sections so that by default the segment has a valid and sensible
1213       // alignment.
1214       PHeader.p_align = 1;
1215       for (const Fragment &F : Fragments)
1216         PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign);
1217     }
1218   }
1219 }
1220 
1221 bool llvm::ELFYAML::shouldAllocateFileSpace(
1222     ArrayRef<ELFYAML::ProgramHeader> Phdrs, const ELFYAML::NoBitsSection &S) {
1223   for (const ELFYAML::ProgramHeader &PH : Phdrs) {
1224     auto It = llvm::find_if(
1225         PH.Chunks, [&](ELFYAML::Chunk *C) { return C->Name == S.Name; });
1226     if (std::any_of(It, PH.Chunks.end(), [](ELFYAML::Chunk *C) {
1227           return (isa<ELFYAML::Fill>(C) ||
1228                   cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1229         }))
1230       return true;
1231   }
1232   return false;
1233 }
1234 
1235 template <class ELFT>
1236 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1237                                          const ELFYAML::NoBitsSection &S,
1238                                          ContiguousBlobAccumulator &CBA) {
1239   if (!S.Size)
1240     return;
1241 
1242   SHeader.sh_size = *S.Size;
1243 
1244   // When a nobits section is followed by a non-nobits section or fill
1245   // in the same segment, we allocate the file space for it. This behavior
1246   // matches linkers.
1247   if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1248     CBA.writeZeros(*S.Size);
1249 }
1250 
1251 template <class ELFT>
1252 void ELFState<ELFT>::writeSectionContent(
1253     Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
1254     ContiguousBlobAccumulator &CBA) {
1255   if (Section.Info)
1256     SHeader.sh_info = *Section.Info;
1257 }
1258 
1259 static bool isMips64EL(const ELFYAML::Object &Obj) {
1260   return Obj.getMachine() == llvm::ELF::EM_MIPS &&
1261          Obj.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
1262          Obj.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1263 }
1264 
1265 template <class ELFT>
1266 void ELFState<ELFT>::writeSectionContent(
1267     Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
1268     ContiguousBlobAccumulator &CBA) {
1269   assert((Section.Type == llvm::ELF::SHT_REL ||
1270           Section.Type == llvm::ELF::SHT_RELA) &&
1271          "Section type is not SHT_REL nor SHT_RELA");
1272 
1273   if (!Section.RelocatableSec.empty())
1274     SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
1275 
1276   if (!Section.Relocations)
1277     return;
1278 
1279   const bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
1280   for (const ELFYAML::Relocation &Rel : *Section.Relocations) {
1281     const bool IsDynamic = Section.Link && (*Section.Link == ".dynsym");
1282     unsigned SymIdx =
1283         Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, IsDynamic) : 0;
1284     if (IsRela) {
1285       Elf_Rela REntry;
1286       zero(REntry);
1287       REntry.r_offset = Rel.Offset;
1288       REntry.r_addend = Rel.Addend;
1289       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1290       CBA.write((const char *)&REntry, sizeof(REntry));
1291     } else {
1292       Elf_Rel REntry;
1293       zero(REntry);
1294       REntry.r_offset = Rel.Offset;
1295       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1296       CBA.write((const char *)&REntry, sizeof(REntry));
1297     }
1298   }
1299 
1300   SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) *
1301                     Section.Relocations->size();
1302 }
1303 
1304 template <class ELFT>
1305 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1306                                          const ELFYAML::RelrSection &Section,
1307                                          ContiguousBlobAccumulator &CBA) {
1308   if (!Section.Entries)
1309     return;
1310 
1311   for (llvm::yaml::Hex64 E : *Section.Entries) {
1312     if (!ELFT::Is64Bits && E > UINT32_MAX)
1313       reportError(Section.Name + ": the value is too large for 32-bits: 0x" +
1314                   Twine::utohexstr(E));
1315     CBA.write<uintX_t>(E, ELFT::TargetEndianness);
1316   }
1317 
1318   SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size();
1319 }
1320 
1321 template <class ELFT>
1322 void ELFState<ELFT>::writeSectionContent(
1323     Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
1324     ContiguousBlobAccumulator &CBA) {
1325   if (Shndx.Content || Shndx.Size) {
1326     SHeader.sh_size = writeContent(CBA, Shndx.Content, Shndx.Size);
1327     return;
1328   }
1329 
1330   if (!Shndx.Entries)
1331     return;
1332 
1333   for (uint32_t E : *Shndx.Entries)
1334     CBA.write<uint32_t>(E, ELFT::TargetEndianness);
1335   SHeader.sh_size = Shndx.Entries->size() * SHeader.sh_entsize;
1336 }
1337 
1338 template <class ELFT>
1339 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1340                                          const ELFYAML::GroupSection &Section,
1341                                          ContiguousBlobAccumulator &CBA) {
1342   assert(Section.Type == llvm::ELF::SHT_GROUP &&
1343          "Section type is not SHT_GROUP");
1344 
1345   if (Section.Signature)
1346     SHeader.sh_info =
1347         toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
1348 
1349   if (!Section.Members)
1350     return;
1351 
1352   for (const ELFYAML::SectionOrType &Member : *Section.Members) {
1353     unsigned int SectionIndex = 0;
1354     if (Member.sectionNameOrType == "GRP_COMDAT")
1355       SectionIndex = llvm::ELF::GRP_COMDAT;
1356     else
1357       SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
1358     CBA.write<uint32_t>(SectionIndex, ELFT::TargetEndianness);
1359   }
1360   SHeader.sh_size = SHeader.sh_entsize * Section.Members->size();
1361 }
1362 
1363 template <class ELFT>
1364 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1365                                          const ELFYAML::SymverSection &Section,
1366                                          ContiguousBlobAccumulator &CBA) {
1367   if (!Section.Entries)
1368     return;
1369 
1370   for (uint16_t Version : *Section.Entries)
1371     CBA.write<uint16_t>(Version, ELFT::TargetEndianness);
1372   SHeader.sh_size = Section.Entries->size() * SHeader.sh_entsize;
1373 }
1374 
1375 template <class ELFT>
1376 void ELFState<ELFT>::writeSectionContent(
1377     Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
1378     ContiguousBlobAccumulator &CBA) {
1379   if (!Section.Entries)
1380     return;
1381 
1382   if (!Section.Entries)
1383     return;
1384 
1385   for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
1386     CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1387     SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(E.Size);
1388   }
1389 }
1390 
1391 template <class ELFT>
1392 void ELFState<ELFT>::writeSectionContent(
1393     Elf_Shdr &SHeader, const ELFYAML::BBAddrMapSection &Section,
1394     ContiguousBlobAccumulator &CBA) {
1395   if (!Section.Entries)
1396     return;
1397 
1398   for (const ELFYAML::BBAddrMapEntry &E : *Section.Entries) {
1399     // Write the address of the function.
1400     CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1401     // Write number of BBEntries (number of basic blocks in the function). This
1402     // is overridden by the 'NumBlocks' YAML field when specified.
1403     uint64_t NumBlocks =
1404         E.NumBlocks.getValueOr(E.BBEntries ? E.BBEntries->size() : 0);
1405     SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1406     // Write all BBEntries.
1407     if (!E.BBEntries)
1408       continue;
1409     for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *E.BBEntries)
1410       SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset) +
1411                          CBA.writeULEB128(BBE.Size) +
1412                          CBA.writeULEB128(BBE.Metadata);
1413   }
1414 }
1415 
1416 template <class ELFT>
1417 void ELFState<ELFT>::writeSectionContent(
1418     Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section,
1419     ContiguousBlobAccumulator &CBA) {
1420   if (!Section.Options)
1421     return;
1422 
1423   for (const ELFYAML::LinkerOption &LO : *Section.Options) {
1424     CBA.write(LO.Key.data(), LO.Key.size());
1425     CBA.write('\0');
1426     CBA.write(LO.Value.data(), LO.Value.size());
1427     CBA.write('\0');
1428     SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2);
1429   }
1430 }
1431 
1432 template <class ELFT>
1433 void ELFState<ELFT>::writeSectionContent(
1434     Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section,
1435     ContiguousBlobAccumulator &CBA) {
1436   if (!Section.Libs)
1437     return;
1438 
1439   for (StringRef Lib : *Section.Libs) {
1440     CBA.write(Lib.data(), Lib.size());
1441     CBA.write('\0');
1442     SHeader.sh_size += Lib.size() + 1;
1443   }
1444 }
1445 
1446 template <class ELFT>
1447 uint64_t
1448 ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
1449                               llvm::Optional<llvm::yaml::Hex64> Offset) {
1450   uint64_t CurrentOffset = CBA.getOffset();
1451   uint64_t AlignedOffset;
1452 
1453   if (Offset) {
1454     if ((uint64_t)*Offset < CurrentOffset) {
1455       reportError("the 'Offset' value (0x" +
1456                   Twine::utohexstr((uint64_t)*Offset) + ") goes backward");
1457       return CurrentOffset;
1458     }
1459 
1460     // We ignore an alignment when an explicit offset has been requested.
1461     AlignedOffset = *Offset;
1462   } else {
1463     AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1));
1464   }
1465 
1466   CBA.writeZeros(AlignedOffset - CurrentOffset);
1467   return AlignedOffset;
1468 }
1469 
1470 template <class ELFT>
1471 void ELFState<ELFT>::writeSectionContent(
1472     Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
1473     ContiguousBlobAccumulator &CBA) {
1474   if (!Section.Entries)
1475     return;
1476 
1477   for (const ELFYAML::CallGraphEntry &E : *Section.Entries) {
1478     unsigned From = toSymbolIndex(E.From, Section.Name, /*IsDynamic=*/false);
1479     unsigned To = toSymbolIndex(E.To, Section.Name, /*IsDynamic=*/false);
1480 
1481     CBA.write<uint32_t>(From, ELFT::TargetEndianness);
1482     CBA.write<uint32_t>(To, ELFT::TargetEndianness);
1483     CBA.write<uint64_t>(E.Weight, ELFT::TargetEndianness);
1484     SHeader.sh_size += 16;
1485   }
1486 }
1487 
1488 template <class ELFT>
1489 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1490                                          const ELFYAML::HashSection &Section,
1491                                          ContiguousBlobAccumulator &CBA) {
1492   if (!Section.Bucket)
1493     return;
1494 
1495   if (!Section.Bucket)
1496     return;
1497 
1498   CBA.write<uint32_t>(
1499       Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())),
1500       ELFT::TargetEndianness);
1501   CBA.write<uint32_t>(
1502       Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())),
1503       ELFT::TargetEndianness);
1504 
1505   for (uint32_t Val : *Section.Bucket)
1506     CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1507   for (uint32_t Val : *Section.Chain)
1508     CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1509 
1510   SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
1511 }
1512 
1513 template <class ELFT>
1514 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1515                                          const ELFYAML::VerdefSection &Section,
1516                                          ContiguousBlobAccumulator &CBA) {
1517 
1518   if (Section.Info)
1519     SHeader.sh_info = *Section.Info;
1520   else if (Section.Entries)
1521     SHeader.sh_info = Section.Entries->size();
1522 
1523   if (!Section.Entries)
1524     return;
1525 
1526   uint64_t AuxCnt = 0;
1527   for (size_t I = 0; I < Section.Entries->size(); ++I) {
1528     const ELFYAML::VerdefEntry &E = (*Section.Entries)[I];
1529 
1530     Elf_Verdef VerDef;
1531     VerDef.vd_version = E.Version.getValueOr(1);
1532     VerDef.vd_flags = E.Flags.getValueOr(0);
1533     VerDef.vd_ndx = E.VersionNdx.getValueOr(0);
1534     VerDef.vd_hash = E.Hash.getValueOr(0);
1535     VerDef.vd_aux = sizeof(Elf_Verdef);
1536     VerDef.vd_cnt = E.VerNames.size();
1537     if (I == Section.Entries->size() - 1)
1538       VerDef.vd_next = 0;
1539     else
1540       VerDef.vd_next =
1541           sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
1542     CBA.write((const char *)&VerDef, sizeof(Elf_Verdef));
1543 
1544     for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
1545       Elf_Verdaux VernAux;
1546       VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
1547       if (J == E.VerNames.size() - 1)
1548         VernAux.vda_next = 0;
1549       else
1550         VernAux.vda_next = sizeof(Elf_Verdaux);
1551       CBA.write((const char *)&VernAux, sizeof(Elf_Verdaux));
1552     }
1553   }
1554 
1555   SHeader.sh_size = Section.Entries->size() * sizeof(Elf_Verdef) +
1556                     AuxCnt * sizeof(Elf_Verdaux);
1557 }
1558 
1559 template <class ELFT>
1560 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1561                                          const ELFYAML::VerneedSection &Section,
1562                                          ContiguousBlobAccumulator &CBA) {
1563   if (Section.Info)
1564     SHeader.sh_info = *Section.Info;
1565   else if (Section.VerneedV)
1566     SHeader.sh_info = Section.VerneedV->size();
1567 
1568   if (!Section.VerneedV)
1569     return;
1570 
1571   uint64_t AuxCnt = 0;
1572   for (size_t I = 0; I < Section.VerneedV->size(); ++I) {
1573     const ELFYAML::VerneedEntry &VE = (*Section.VerneedV)[I];
1574 
1575     Elf_Verneed VerNeed;
1576     VerNeed.vn_version = VE.Version;
1577     VerNeed.vn_file = DotDynstr.getOffset(VE.File);
1578     if (I == Section.VerneedV->size() - 1)
1579       VerNeed.vn_next = 0;
1580     else
1581       VerNeed.vn_next =
1582           sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
1583     VerNeed.vn_cnt = VE.AuxV.size();
1584     VerNeed.vn_aux = sizeof(Elf_Verneed);
1585     CBA.write((const char *)&VerNeed, sizeof(Elf_Verneed));
1586 
1587     for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
1588       const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
1589 
1590       Elf_Vernaux VernAux;
1591       VernAux.vna_hash = VAuxE.Hash;
1592       VernAux.vna_flags = VAuxE.Flags;
1593       VernAux.vna_other = VAuxE.Other;
1594       VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
1595       if (J == VE.AuxV.size() - 1)
1596         VernAux.vna_next = 0;
1597       else
1598         VernAux.vna_next = sizeof(Elf_Vernaux);
1599       CBA.write((const char *)&VernAux, sizeof(Elf_Vernaux));
1600     }
1601   }
1602 
1603   SHeader.sh_size = Section.VerneedV->size() * sizeof(Elf_Verneed) +
1604                     AuxCnt * sizeof(Elf_Vernaux);
1605 }
1606 
1607 template <class ELFT>
1608 void ELFState<ELFT>::writeSectionContent(
1609     Elf_Shdr &SHeader, const ELFYAML::ARMIndexTableSection &Section,
1610     ContiguousBlobAccumulator &CBA) {
1611   if (!Section.Entries)
1612     return;
1613 
1614   for (const ELFYAML::ARMIndexTableEntry &E : *Section.Entries) {
1615     CBA.write<uint32_t>(E.Offset, ELFT::TargetEndianness);
1616     CBA.write<uint32_t>(E.Value, ELFT::TargetEndianness);
1617   }
1618   SHeader.sh_size = Section.Entries->size() * 8;
1619 }
1620 
1621 template <class ELFT>
1622 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1623                                          const ELFYAML::MipsABIFlags &Section,
1624                                          ContiguousBlobAccumulator &CBA) {
1625   assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
1626          "Section type is not SHT_MIPS_ABIFLAGS");
1627 
1628   object::Elf_Mips_ABIFlags<ELFT> Flags;
1629   zero(Flags);
1630   SHeader.sh_size = SHeader.sh_entsize;
1631 
1632   Flags.version = Section.Version;
1633   Flags.isa_level = Section.ISALevel;
1634   Flags.isa_rev = Section.ISARevision;
1635   Flags.gpr_size = Section.GPRSize;
1636   Flags.cpr1_size = Section.CPR1Size;
1637   Flags.cpr2_size = Section.CPR2Size;
1638   Flags.fp_abi = Section.FpABI;
1639   Flags.isa_ext = Section.ISAExtension;
1640   Flags.ases = Section.ASEs;
1641   Flags.flags1 = Section.Flags1;
1642   Flags.flags2 = Section.Flags2;
1643   CBA.write((const char *)&Flags, sizeof(Flags));
1644 }
1645 
1646 template <class ELFT>
1647 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1648                                          const ELFYAML::DynamicSection &Section,
1649                                          ContiguousBlobAccumulator &CBA) {
1650   assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
1651          "Section type is not SHT_DYNAMIC");
1652 
1653   if (!Section.Entries)
1654     return;
1655 
1656   for (const ELFYAML::DynamicEntry &DE : *Section.Entries) {
1657     CBA.write<uintX_t>(DE.Tag, ELFT::TargetEndianness);
1658     CBA.write<uintX_t>(DE.Val, ELFT::TargetEndianness);
1659   }
1660   SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries->size();
1661 }
1662 
1663 template <class ELFT>
1664 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1665                                          const ELFYAML::AddrsigSection &Section,
1666                                          ContiguousBlobAccumulator &CBA) {
1667   if (!Section.Symbols)
1668     return;
1669 
1670   if (!Section.Symbols)
1671     return;
1672 
1673   for (StringRef Sym : *Section.Symbols)
1674     SHeader.sh_size +=
1675         CBA.writeULEB128(toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false));
1676 }
1677 
1678 template <class ELFT>
1679 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1680                                          const ELFYAML::NoteSection &Section,
1681                                          ContiguousBlobAccumulator &CBA) {
1682   if (!Section.Notes)
1683     return;
1684 
1685   uint64_t Offset = CBA.tell();
1686   for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
1687     // Write name size.
1688     if (NE.Name.empty())
1689       CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1690     else
1691       CBA.write<uint32_t>(NE.Name.size() + 1, ELFT::TargetEndianness);
1692 
1693     // Write description size.
1694     if (NE.Desc.binary_size() == 0)
1695       CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1696     else
1697       CBA.write<uint32_t>(NE.Desc.binary_size(), ELFT::TargetEndianness);
1698 
1699     // Write type.
1700     CBA.write<uint32_t>(NE.Type, ELFT::TargetEndianness);
1701 
1702     // Write name, null terminator and padding.
1703     if (!NE.Name.empty()) {
1704       CBA.write(NE.Name.data(), NE.Name.size());
1705       CBA.write('\0');
1706       CBA.padToAlignment(4);
1707     }
1708 
1709     // Write description and padding.
1710     if (NE.Desc.binary_size() != 0) {
1711       CBA.writeAsBinary(NE.Desc);
1712       CBA.padToAlignment(4);
1713     }
1714   }
1715 
1716   SHeader.sh_size = CBA.tell() - Offset;
1717 }
1718 
1719 template <class ELFT>
1720 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1721                                          const ELFYAML::GnuHashSection &Section,
1722                                          ContiguousBlobAccumulator &CBA) {
1723   if (!Section.HashBuckets)
1724     return;
1725 
1726   if (!Section.Header)
1727     return;
1728 
1729   // We write the header first, starting with the hash buckets count. Normally
1730   // it is the number of entries in HashBuckets, but the "NBuckets" property can
1731   // be used to override this field, which is useful for producing broken
1732   // objects.
1733   if (Section.Header->NBuckets)
1734     CBA.write<uint32_t>(*Section.Header->NBuckets, ELFT::TargetEndianness);
1735   else
1736     CBA.write<uint32_t>(Section.HashBuckets->size(), ELFT::TargetEndianness);
1737 
1738   // Write the index of the first symbol in the dynamic symbol table accessible
1739   // via the hash table.
1740   CBA.write<uint32_t>(Section.Header->SymNdx, ELFT::TargetEndianness);
1741 
1742   // Write the number of words in the Bloom filter. As above, the "MaskWords"
1743   // property can be used to set this field to any value.
1744   if (Section.Header->MaskWords)
1745     CBA.write<uint32_t>(*Section.Header->MaskWords, ELFT::TargetEndianness);
1746   else
1747     CBA.write<uint32_t>(Section.BloomFilter->size(), ELFT::TargetEndianness);
1748 
1749   // Write the shift constant used by the Bloom filter.
1750   CBA.write<uint32_t>(Section.Header->Shift2, ELFT::TargetEndianness);
1751 
1752   // We've finished writing the header. Now write the Bloom filter.
1753   for (llvm::yaml::Hex64 Val : *Section.BloomFilter)
1754     CBA.write<uintX_t>(Val, ELFT::TargetEndianness);
1755 
1756   // Write an array of hash buckets.
1757   for (llvm::yaml::Hex32 Val : *Section.HashBuckets)
1758     CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1759 
1760   // Write an array of hash values.
1761   for (llvm::yaml::Hex32 Val : *Section.HashValues)
1762     CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1763 
1764   SHeader.sh_size = 16 /*Header size*/ +
1765                     Section.BloomFilter->size() * sizeof(typename ELFT::uint) +
1766                     Section.HashBuckets->size() * 4 +
1767                     Section.HashValues->size() * 4;
1768 }
1769 
1770 template <class ELFT>
1771 void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill,
1772                                ContiguousBlobAccumulator &CBA) {
1773   size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0;
1774   if (!PatternSize) {
1775     CBA.writeZeros(Fill.Size);
1776     return;
1777   }
1778 
1779   // Fill the content with the specified pattern.
1780   uint64_t Written = 0;
1781   for (; Written + PatternSize <= Fill.Size; Written += PatternSize)
1782     CBA.writeAsBinary(*Fill.Pattern);
1783   CBA.writeAsBinary(*Fill.Pattern, Fill.Size - Written);
1784 }
1785 
1786 template <class ELFT>
1787 DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() {
1788   const ELFYAML::SectionHeaderTable &SectionHeaders =
1789       Doc.getSectionHeaderTable();
1790   if (SectionHeaders.IsImplicit || SectionHeaders.NoHeaders ||
1791       SectionHeaders.isDefault())
1792     return DenseMap<StringRef, size_t>();
1793 
1794   DenseMap<StringRef, size_t> Ret;
1795   size_t SecNdx = 0;
1796   StringSet<> Seen;
1797 
1798   auto AddSection = [&](const ELFYAML::SectionHeader &Hdr) {
1799     if (!Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1800       reportError("repeated section name: '" + Hdr.Name +
1801                   "' in the section header description");
1802     Seen.insert(Hdr.Name);
1803   };
1804 
1805   if (SectionHeaders.Sections)
1806     for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Sections)
1807       AddSection(Hdr);
1808 
1809   if (SectionHeaders.Excluded)
1810     for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Excluded)
1811       AddSection(Hdr);
1812 
1813   for (const ELFYAML::Section *S : Doc.getSections()) {
1814     // Ignore special first SHT_NULL section.
1815     if (S == Doc.getSections().front())
1816       continue;
1817     if (!Seen.count(S->Name))
1818       reportError("section '" + S->Name +
1819                   "' should be present in the 'Sections' or 'Excluded' lists");
1820     Seen.erase(S->Name);
1821   }
1822 
1823   for (const auto &It : Seen)
1824     reportError("section header contains undefined section '" + It.getKey() +
1825                 "'");
1826   return Ret;
1827 }
1828 
1829 template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
1830   // A YAML description can have an explicit section header declaration that
1831   // allows to change the order of section headers.
1832   DenseMap<StringRef, size_t> ReorderMap = buildSectionHeaderReorderMap();
1833 
1834   if (HasError)
1835     return;
1836 
1837   // Build excluded section headers map.
1838   std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1839   const ELFYAML::SectionHeaderTable &SectionHeaders =
1840       Doc.getSectionHeaderTable();
1841   if (SectionHeaders.Excluded)
1842     for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Excluded)
1843       if (!ExcludedSectionHeaders.insert(Hdr.Name).second)
1844         llvm_unreachable("buildSectionIndex() failed");
1845 
1846   if (SectionHeaders.NoHeaders.getValueOr(false))
1847     for (const ELFYAML::Section *S : Sections)
1848       if (!ExcludedSectionHeaders.insert(S->Name).second)
1849         llvm_unreachable("buildSectionIndex() failed");
1850 
1851   size_t SecNdx = -1;
1852   for (const ELFYAML::Section *S : Sections) {
1853     ++SecNdx;
1854 
1855     size_t Index = ReorderMap.empty() ? SecNdx : ReorderMap.lookup(S->Name);
1856     if (!SN2I.addName(S->Name, Index))
1857       llvm_unreachable("buildSectionIndex() failed");
1858 
1859     if (!ExcludedSectionHeaders.count(S->Name))
1860       ShStrtabStrings->add(ELFYAML::dropUniqueSuffix(S->Name));
1861   }
1862 }
1863 
1864 template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
1865   auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
1866     for (size_t I = 0, S = V.size(); I < S; ++I) {
1867       const ELFYAML::Symbol &Sym = V[I];
1868       if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
1869         reportError("repeated symbol name: '" + Sym.Name + "'");
1870     }
1871   };
1872 
1873   if (Doc.Symbols)
1874     Build(*Doc.Symbols, SymN2I);
1875   if (Doc.DynamicSymbols)
1876     Build(*Doc.DynamicSymbols, DynSymN2I);
1877 }
1878 
1879 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
1880   // Add the regular symbol names to .strtab section.
1881   if (Doc.Symbols)
1882     for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
1883       DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1884   DotStrtab.finalize();
1885 
1886   // Add the dynamic symbol names to .dynstr section.
1887   if (Doc.DynamicSymbols)
1888     for (const ELFYAML::Symbol &Sym : *Doc.DynamicSymbols)
1889       DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1890 
1891   // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1892   // add strings to .dynstr section.
1893   for (const ELFYAML::Chunk *Sec : Doc.getSections()) {
1894     if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1895       if (VerNeed->VerneedV) {
1896         for (const ELFYAML::VerneedEntry &VE : *VerNeed->VerneedV) {
1897           DotDynstr.add(VE.File);
1898           for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1899             DotDynstr.add(Aux.Name);
1900         }
1901       }
1902     } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1903       if (VerDef->Entries)
1904         for (const ELFYAML::VerdefEntry &E : *VerDef->Entries)
1905           for (StringRef Name : E.VerNames)
1906             DotDynstr.add(Name);
1907     }
1908   }
1909 
1910   DotDynstr.finalize();
1911 
1912   // Don't finalize the section header string table a second time if it has
1913   // already been finalized due to being one of the symbol string tables.
1914   if (ShStrtabStrings != &DotStrtab && ShStrtabStrings != &DotDynstr)
1915     ShStrtabStrings->finalize();
1916 }
1917 
1918 template <class ELFT>
1919 bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
1920                               yaml::ErrorHandler EH, uint64_t MaxSize) {
1921   ELFState<ELFT> State(Doc, EH);
1922   if (State.HasError)
1923     return false;
1924 
1925   // Build the section index, which adds sections to the section header string
1926   // table first, so that we can finalize the section header string table.
1927   State.buildSectionIndex();
1928   State.buildSymbolIndexes();
1929 
1930   // Finalize section header string table and the .strtab and .dynstr sections.
1931   // We do this early because we want to finalize the string table builders
1932   // before writing the content of the sections that might want to use them.
1933   State.finalizeStrings();
1934 
1935   if (State.HasError)
1936     return false;
1937 
1938   std::vector<Elf_Phdr> PHeaders;
1939   State.initProgramHeaders(PHeaders);
1940 
1941   // XXX: This offset is tightly coupled with the order that we write
1942   // things to `OS`.
1943   const size_t SectionContentBeginOffset =
1944       sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
1945   // It is quite easy to accidentally create output with yaml2obj that is larger
1946   // than intended, for example, due to an issue in the YAML description.
1947   // We limit the maximum allowed output size, but also provide a command line
1948   // option to change this limitation.
1949   ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
1950 
1951   std::vector<Elf_Shdr> SHeaders;
1952   State.initSectionHeaders(SHeaders, CBA);
1953 
1954   // Now we can decide segment offsets.
1955   State.setProgramHeaderLayout(PHeaders, SHeaders);
1956 
1957   bool ReachedLimit = CBA.getOffset() > MaxSize;
1958   if (Error E = CBA.takeLimitError()) {
1959     // We report a custom error message instead below.
1960     consumeError(std::move(E));
1961     ReachedLimit = true;
1962   }
1963 
1964   if (ReachedLimit)
1965     State.reportError(
1966         "the desired output size is greater than permitted. Use the "
1967         "--max-size option to change the limit");
1968 
1969   if (State.HasError)
1970     return false;
1971 
1972   State.writeELFHeader(OS);
1973   writeArrayData(OS, makeArrayRef(PHeaders));
1974 
1975   const ELFYAML::SectionHeaderTable &SHT = Doc.getSectionHeaderTable();
1976   if (!SHT.NoHeaders.getValueOr(false))
1977     CBA.updateDataAt(*SHT.Offset, SHeaders.data(),
1978                      SHT.getNumHeaders(SHeaders.size()) * sizeof(Elf_Shdr));
1979 
1980   CBA.writeBlobToStream(OS);
1981   return true;
1982 }
1983 
1984 namespace llvm {
1985 namespace yaml {
1986 
1987 bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH,
1988               uint64_t MaxSize) {
1989   bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1990   bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1991   if (Is64Bit) {
1992     if (IsLE)
1993       return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
1994     return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
1995   }
1996   if (IsLE)
1997     return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
1998   return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH, MaxSize);
1999 }
2000 
2001 } // namespace yaml
2002 } // namespace llvm
2003