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