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/StringSet.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/MC/StringTableBuilder.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #include "llvm/ObjectYAML/ELFYAML.h"
20 #include "llvm/ObjectYAML/yaml2obj.h"
21 #include "llvm/Support/EndianStream.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/WithColor.h"
24 #include "llvm/Support/YAMLTraits.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 using namespace llvm;
28 
29 // This class is used to build up a contiguous binary blob while keeping
30 // track of an offset in the output (which notionally begins at
31 // `InitialOffset`).
32 namespace {
33 class ContiguousBlobAccumulator {
34   const uint64_t InitialOffset;
35   SmallVector<char, 128> Buf;
36   raw_svector_ostream OS;
37 
38   /// \returns The new offset.
39   uint64_t padToAlignment(unsigned Align) {
40     if (Align == 0)
41       Align = 1;
42     uint64_t CurrentOffset = InitialOffset + OS.tell();
43     uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
44     OS.write_zeros(AlignedOffset - CurrentOffset);
45     return AlignedOffset; // == CurrentOffset;
46   }
47 
48 public:
49   ContiguousBlobAccumulator(uint64_t InitialOffset_)
50       : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
51   template <class Integer>
52   raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
53     Offset = padToAlignment(Align);
54     return OS;
55   }
56   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
57 };
58 
59 // Used to keep track of section and symbol names, so that in the YAML file
60 // sections and symbols can be referenced by name instead of by index.
61 class NameToIdxMap {
62   StringMap<unsigned> Map;
63 
64 public:
65   /// \Returns false if name is already present in the map.
66   bool addName(StringRef Name, unsigned Ndx) {
67     return Map.insert({Name, Ndx}).second;
68   }
69   /// \Returns false if name is not present in the map.
70   bool lookup(StringRef Name, unsigned &Idx) const {
71     auto I = Map.find(Name);
72     if (I == Map.end())
73       return false;
74     Idx = I->getValue();
75     return true;
76   }
77   /// Asserts if name is not present in the map.
78   unsigned get(StringRef Name) const {
79     unsigned Idx;
80     if (lookup(Name, Idx))
81       return Idx;
82     assert(false && "Expected section not found in index");
83     return 0;
84   }
85   unsigned size() const { return Map.size(); }
86 };
87 
88 /// "Single point of truth" for the ELF file construction.
89 /// TODO: This class still has a ways to go before it is truly a "single
90 /// point of truth".
91 template <class ELFT> class ELFState {
92   typedef typename ELFT::Ehdr Elf_Ehdr;
93   typedef typename ELFT::Phdr Elf_Phdr;
94   typedef typename ELFT::Shdr Elf_Shdr;
95   typedef typename ELFT::Sym Elf_Sym;
96   typedef typename ELFT::Rel Elf_Rel;
97   typedef typename ELFT::Rela Elf_Rela;
98   typedef typename ELFT::Relr Elf_Relr;
99   typedef typename ELFT::Dyn Elf_Dyn;
100 
101   enum class SymtabType { Static, Dynamic };
102 
103   /// The future ".strtab" section.
104   StringTableBuilder DotStrtab{StringTableBuilder::ELF};
105 
106   /// The future ".shstrtab" section.
107   StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
108 
109   /// The future ".dynstr" section.
110   StringTableBuilder DotDynstr{StringTableBuilder::ELF};
111 
112   NameToIdxMap SN2I;
113   NameToIdxMap SymN2I;
114   ELFYAML::Object &Doc;
115 
116   bool buildSectionIndex();
117   bool buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols);
118   void initELFHeader(Elf_Ehdr &Header);
119   void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
120   bool initImplicitHeader(ELFState<ELFT> &State, ContiguousBlobAccumulator &CBA,
121                           Elf_Shdr &Header, StringRef SecName,
122                           ELFYAML::Section *YAMLSec);
123   bool initSectionHeaders(ELFState<ELFT> &State,
124                           std::vector<Elf_Shdr> &SHeaders,
125                           ContiguousBlobAccumulator &CBA);
126   void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
127                                ContiguousBlobAccumulator &CBA,
128                                ELFYAML::Section *YAMLSec);
129   void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
130                                StringTableBuilder &STB,
131                                ContiguousBlobAccumulator &CBA,
132                                ELFYAML::Section *YAMLSec);
133   void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
134                               std::vector<Elf_Shdr> &SHeaders);
135   bool writeSectionContent(Elf_Shdr &SHeader,
136                            const ELFYAML::RawContentSection &Section,
137                            ContiguousBlobAccumulator &CBA);
138   bool writeSectionContent(Elf_Shdr &SHeader,
139                            const ELFYAML::RelocationSection &Section,
140                            ContiguousBlobAccumulator &CBA);
141   bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
142                            ContiguousBlobAccumulator &CBA);
143   bool writeSectionContent(Elf_Shdr &SHeader,
144                            const ELFYAML::SymtabShndxSection &Shndx,
145                            ContiguousBlobAccumulator &CBA);
146   bool writeSectionContent(Elf_Shdr &SHeader,
147                            const ELFYAML::SymverSection &Section,
148                            ContiguousBlobAccumulator &CBA);
149   bool writeSectionContent(Elf_Shdr &SHeader,
150                            const ELFYAML::VerneedSection &Section,
151                            ContiguousBlobAccumulator &CBA);
152   bool writeSectionContent(Elf_Shdr &SHeader,
153                            const ELFYAML::VerdefSection &Section,
154                            ContiguousBlobAccumulator &CBA);
155   bool writeSectionContent(Elf_Shdr &SHeader,
156                            const ELFYAML::MipsABIFlags &Section,
157                            ContiguousBlobAccumulator &CBA);
158   bool writeSectionContent(Elf_Shdr &SHeader,
159                            const ELFYAML::DynamicSection &Section,
160                            ContiguousBlobAccumulator &CBA);
161   ELFState(ELFYAML::Object &D);
162 
163 public:
164   static int writeELF(raw_ostream &OS, ELFYAML::Object &Doc);
165 
166 private:
167   void finalizeStrings();
168 };
169 } // end anonymous namespace
170 
171 template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
172   return A.size() * sizeof(T);
173 }
174 
175 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
176   OS.write((const char *)A.data(), arrayDataSize(A));
177 }
178 
179 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
180 
181 template <class ELFT> ELFState<ELFT>::ELFState(ELFYAML::Object &D) : Doc(D) {
182   StringSet<> DocSections;
183   for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
184     if (!D->Name.empty())
185       DocSections.insert(D->Name);
186 
187   // Insert SHT_NULL section implicitly when it is not defined in YAML.
188   if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
189     Doc.Sections.insert(
190         Doc.Sections.begin(),
191         llvm::make_unique<ELFYAML::Section>(
192             ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
193 
194   std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
195   if (!Doc.DynamicSymbols.empty())
196     ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
197 
198   // Insert placeholders for implicit sections that are not
199   // defined explicitly in YAML.
200   for (StringRef SecName : ImplicitSections) {
201     if (DocSections.count(SecName))
202       continue;
203 
204     std::unique_ptr<ELFYAML::Section> Sec = llvm::make_unique<ELFYAML::Section>(
205         ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/);
206     Sec->Name = SecName;
207     Doc.Sections.push_back(std::move(Sec));
208   }
209 }
210 
211 template <class ELFT> void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
212   using namespace llvm::ELF;
213   zero(Header);
214   Header.e_ident[EI_MAG0] = 0x7f;
215   Header.e_ident[EI_MAG1] = 'E';
216   Header.e_ident[EI_MAG2] = 'L';
217   Header.e_ident[EI_MAG3] = 'F';
218   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
219   Header.e_ident[EI_DATA] = Doc.Header.Data;
220   Header.e_ident[EI_VERSION] = EV_CURRENT;
221   Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
222   Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
223   Header.e_type = Doc.Header.Type;
224   Header.e_machine = Doc.Header.Machine;
225   Header.e_version = EV_CURRENT;
226   Header.e_entry = Doc.Header.Entry;
227   Header.e_phoff = sizeof(Header);
228   Header.e_flags = Doc.Header.Flags;
229   Header.e_ehsize = sizeof(Elf_Ehdr);
230   Header.e_phentsize = sizeof(Elf_Phdr);
231   Header.e_phnum = Doc.ProgramHeaders.size();
232 
233   Header.e_shentsize =
234       Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
235   // Immediately following the ELF header and program headers.
236   Header.e_shoff =
237       Doc.Header.SHOffset
238           ? (typename ELFT::uint)(*Doc.Header.SHOffset)
239           : sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
240   Header.e_shnum =
241       Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
242   Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
243                                           : SN2I.get(".shstrtab");
244 }
245 
246 template <class ELFT>
247 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
248   for (const auto &YamlPhdr : Doc.ProgramHeaders) {
249     Elf_Phdr Phdr;
250     Phdr.p_type = YamlPhdr.Type;
251     Phdr.p_flags = YamlPhdr.Flags;
252     Phdr.p_vaddr = YamlPhdr.VAddr;
253     Phdr.p_paddr = YamlPhdr.PAddr;
254     PHeaders.push_back(Phdr);
255   }
256 }
257 
258 static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
259                                 StringRef IndexSrc, unsigned &IndexDest) {
260   if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
261     WithColor::error() << "Unknown section referenced: '" << IndexSrc
262                        << "' at YAML section '" << SecName << "'.\n";
263     return false;
264   }
265   return true;
266 }
267 
268 template <class ELFT>
269 bool ELFState<ELFT>::initImplicitHeader(ELFState<ELFT> &State,
270                                         ContiguousBlobAccumulator &CBA,
271                                         Elf_Shdr &Header, StringRef SecName,
272                                         ELFYAML::Section *YAMLSec) {
273   // Check if the header was already initialized.
274   if (Header.sh_offset)
275     return false;
276 
277   if (SecName == ".symtab")
278     State.initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
279   else if (SecName == ".strtab")
280     State.initStrtabSectionHeader(Header, SecName, State.DotStrtab, CBA,
281                                   YAMLSec);
282   else if (SecName == ".shstrtab")
283     State.initStrtabSectionHeader(Header, SecName, State.DotShStrtab, CBA,
284                                   YAMLSec);
285 
286   else if (SecName == ".dynsym")
287     State.initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
288   else if (SecName == ".dynstr")
289     State.initStrtabSectionHeader(Header, SecName, State.DotDynstr, CBA,
290                                   YAMLSec);
291   else
292     return false;
293 
294   // Override the sh_offset/sh_size fields if requested.
295   if (YAMLSec) {
296     if (YAMLSec->ShOffset)
297       Header.sh_offset = *YAMLSec->ShOffset;
298     if (YAMLSec->ShSize)
299       Header.sh_size = *YAMLSec->ShSize;
300   }
301 
302   return true;
303 }
304 
305 static StringRef dropUniqueSuffix(StringRef S) {
306   size_t SuffixPos = S.rfind(" [");
307   if (SuffixPos == StringRef::npos)
308     return S;
309   return S.substr(0, SuffixPos);
310 }
311 
312 template <class ELFT>
313 bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
314                                         std::vector<Elf_Shdr> &SHeaders,
315                                         ContiguousBlobAccumulator &CBA) {
316   // Ensure SHN_UNDEF entry is present. An all-zero section header is a
317   // valid SHN_UNDEF entry since SHT_NULL == 0.
318   SHeaders.resize(Doc.Sections.size());
319 
320   for (size_t I = 0; I < Doc.Sections.size(); ++I) {
321     ELFYAML::Section *Sec = Doc.Sections[I].get();
322     if (I == 0 && Sec->IsImplicit)
323       continue;
324 
325     // We have a few sections like string or symbol tables that are usually
326     // added implicitly to the end. However, if they are explicitly specified
327     // in the YAML, we need to write them here. This ensures the file offset
328     // remains correct.
329     Elf_Shdr &SHeader = SHeaders[I];
330     if (initImplicitHeader(State, CBA, SHeader, Sec->Name,
331                            Sec->IsImplicit ? nullptr : Sec))
332       continue;
333 
334     assert(Sec && "It can't be null unless it is an implicit section. But all "
335                   "implicit sections should already have been handled above.");
336 
337     SHeader.sh_name = DotShStrtab.getOffset(dropUniqueSuffix(Sec->Name));
338     SHeader.sh_type = Sec->Type;
339     if (Sec->Flags)
340       SHeader.sh_flags = *Sec->Flags;
341     SHeader.sh_addr = Sec->Address;
342     SHeader.sh_addralign = Sec->AddressAlign;
343 
344     if (!Sec->Link.empty()) {
345       unsigned Index;
346       if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
347         return false;
348       SHeader.sh_link = Index;
349     }
350 
351     if (I == 0) {
352       if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
353         // We do not write any content for special SHN_UNDEF section.
354         if (RawSec->Size)
355           SHeader.sh_size = *RawSec->Size;
356         if (RawSec->Info)
357           SHeader.sh_info = *RawSec->Info;
358       }
359       if (Sec->EntSize)
360         SHeader.sh_entsize = *Sec->EntSize;
361     } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
362       if (!writeSectionContent(SHeader, *S, CBA))
363         return false;
364     } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
365       if (!writeSectionContent(SHeader, *S, CBA))
366         return false;
367     } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
368       if (!writeSectionContent(SHeader, *S, CBA))
369         return false;
370     } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
371       if (!writeSectionContent(SHeader, *S, CBA))
372         return false;
373     } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
374       if (!writeSectionContent(SHeader, *S, CBA))
375         return false;
376     } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
377       SHeader.sh_entsize = 0;
378       SHeader.sh_size = S->Size;
379       // SHT_NOBITS section does not have content
380       // so just to setup the section offset.
381       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
382     } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
383       if (!writeSectionContent(SHeader, *S, CBA))
384         return false;
385     } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
386       if (!writeSectionContent(SHeader, *S, CBA))
387         return false;
388     } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
389       if (!writeSectionContent(SHeader, *S, CBA))
390         return false;
391     } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
392       if (!writeSectionContent(SHeader, *S, CBA))
393         return false;
394     } else
395       llvm_unreachable("Unknown section type");
396 
397     // Override the sh_offset/sh_size fields if requested.
398     if (Sec) {
399       if (Sec->ShOffset)
400         SHeader.sh_offset = *Sec->ShOffset;
401       if (Sec->ShSize)
402         SHeader.sh_size = *Sec->ShSize;
403     }
404   }
405 
406   return true;
407 }
408 
409 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
410   for (size_t I = 0; I < Symbols.size(); ++I)
411     if (Symbols[I].Binding.value != ELF::STB_LOCAL)
412       return I;
413   return Symbols.size();
414 }
415 
416 static uint64_t writeRawSectionData(raw_ostream &OS,
417                                     const ELFYAML::RawContentSection &RawSec) {
418   size_t ContentSize = 0;
419   if (RawSec.Content) {
420     RawSec.Content->writeAsBinary(OS);
421     ContentSize = RawSec.Content->binary_size();
422   }
423 
424   if (!RawSec.Size)
425     return ContentSize;
426 
427   OS.write_zeros(*RawSec.Size - ContentSize);
428   return *RawSec.Size;
429 }
430 
431 template <class ELFT>
432 static std::vector<typename ELFT::Sym>
433 toELFSymbols(NameToIdxMap &SN2I, ArrayRef<ELFYAML::Symbol> Symbols,
434              const StringTableBuilder &Strtab) {
435   using Elf_Sym = typename ELFT::Sym;
436 
437   std::vector<Elf_Sym> Ret;
438   Ret.resize(Symbols.size() + 1);
439 
440   size_t I = 0;
441   for (const auto &Sym : Symbols) {
442     Elf_Sym &Symbol = Ret[++I];
443 
444     // If NameIndex, which contains the name offset, is explicitly specified, we
445     // use it. This is useful for preparing broken objects. Otherwise, we add
446     // the specified Name to the string table builder to get its offset.
447     if (Sym.NameIndex)
448       Symbol.st_name = *Sym.NameIndex;
449     else if (!Sym.Name.empty())
450       Symbol.st_name = Strtab.getOffset(dropUniqueSuffix(Sym.Name));
451 
452     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
453     if (!Sym.Section.empty()) {
454       unsigned Index;
455       if (!SN2I.lookup(Sym.Section, Index)) {
456         WithColor::error() << "Unknown section referenced: '" << Sym.Section
457                            << "' by YAML symbol " << Sym.Name << ".\n";
458         exit(1);
459       }
460       Symbol.st_shndx = Index;
461     } else if (Sym.Index) {
462       Symbol.st_shndx = *Sym.Index;
463     }
464     // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
465     Symbol.st_value = Sym.Value;
466     Symbol.st_other = Sym.Other;
467     Symbol.st_size = Sym.Size;
468   }
469 
470   return Ret;
471 }
472 
473 template <class ELFT>
474 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
475                                              SymtabType STType,
476                                              ContiguousBlobAccumulator &CBA,
477                                              ELFYAML::Section *YAMLSec) {
478 
479   bool IsStatic = STType == SymtabType::Static;
480   const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
481 
482   ELFYAML::RawContentSection *RawSec =
483       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
484   if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
485     if (RawSec->Content)
486       WithColor::error() << "Cannot specify both `Content` and " +
487                                 (IsStatic ? Twine("`Symbols`")
488                                           : Twine("`DynamicSymbols`")) +
489                                 " for symbol table section '"
490                          << RawSec->Name << "'.\n";
491     if (RawSec->Size)
492       WithColor::error() << "Cannot specify both `Size` and " +
493                                 (IsStatic ? Twine("`Symbols`")
494                                           : Twine("`DynamicSymbols`")) +
495                                 " for symbol table section '"
496                          << RawSec->Name << "'.\n";
497     exit(1);
498   }
499 
500   zero(SHeader);
501   SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
502 
503   if (YAMLSec)
504     SHeader.sh_type = YAMLSec->Type;
505   else
506     SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
507 
508   if (RawSec && !RawSec->Link.empty()) {
509     // If the Link field is explicitly defined in the document,
510     // we should use it.
511     unsigned Index;
512     if (!convertSectionIndex(SN2I, RawSec->Name, RawSec->Link, Index))
513       return;
514     SHeader.sh_link = Index;
515   } else {
516     // When we describe the .dynsym section in the document explicitly, it is
517     // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
518     // added implicitly and we should be able to leave the Link zeroed if
519     // .dynstr is not defined.
520     unsigned Link = 0;
521     if (IsStatic)
522       Link = SN2I.get(".strtab");
523     else
524       SN2I.lookup(".dynstr", Link);
525     SHeader.sh_link = Link;
526   }
527 
528   if (YAMLSec && YAMLSec->Flags)
529     SHeader.sh_flags = *YAMLSec->Flags;
530   else if (!IsStatic)
531     SHeader.sh_flags = ELF::SHF_ALLOC;
532 
533   // If the symbol table section is explicitly described in the YAML
534   // then we should set the fields requested.
535   SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
536                                              : findFirstNonGlobal(Symbols) + 1;
537   SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
538                            ? (uint64_t)(*YAMLSec->EntSize)
539                            : sizeof(Elf_Sym);
540   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
541   SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
542 
543   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
544   if (RawSec && (RawSec->Content || RawSec->Size)) {
545     assert(Symbols.empty());
546     SHeader.sh_size = writeRawSectionData(OS, *RawSec);
547     return;
548   }
549 
550   std::vector<Elf_Sym> Syms =
551       toELFSymbols<ELFT>(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr);
552   writeArrayData(OS, makeArrayRef(Syms));
553   SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
554 }
555 
556 template <class ELFT>
557 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
558                                              StringTableBuilder &STB,
559                                              ContiguousBlobAccumulator &CBA,
560                                              ELFYAML::Section *YAMLSec) {
561   zero(SHeader);
562   SHeader.sh_name = DotShStrtab.getOffset(Name);
563   SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
564   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
565 
566   ELFYAML::RawContentSection *RawSec =
567       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
568 
569   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
570   if (RawSec && (RawSec->Content || RawSec->Size)) {
571     SHeader.sh_size = writeRawSectionData(OS, *RawSec);
572   } else {
573     STB.write(OS);
574     SHeader.sh_size = STB.getSize();
575   }
576 
577   if (YAMLSec && YAMLSec->EntSize)
578     SHeader.sh_entsize = *YAMLSec->EntSize;
579 
580   if (RawSec && RawSec->Info)
581     SHeader.sh_info = *RawSec->Info;
582 
583   if (YAMLSec && YAMLSec->Flags)
584     SHeader.sh_flags = *YAMLSec->Flags;
585   else if (Name == ".dynstr")
586     SHeader.sh_flags = ELF::SHF_ALLOC;
587 
588   // If the section is explicitly described in the YAML
589   // then we want to use its section address.
590   if (YAMLSec)
591     SHeader.sh_addr = YAMLSec->Address;
592 }
593 
594 template <class ELFT>
595 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
596                                             std::vector<Elf_Shdr> &SHeaders) {
597   uint32_t PhdrIdx = 0;
598   for (auto &YamlPhdr : Doc.ProgramHeaders) {
599     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
600 
601     std::vector<Elf_Shdr *> Sections;
602     for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
603       unsigned Index;
604       if (!SN2I.lookup(SecName.Section, Index)) {
605         WithColor::error() << "Unknown section referenced: '" << SecName.Section
606                            << "' by program header.\n";
607         exit(1);
608       }
609       Sections.push_back(&SHeaders[Index]);
610     }
611 
612     if (YamlPhdr.Offset) {
613       PHeader.p_offset = *YamlPhdr.Offset;
614     } else {
615       if (YamlPhdr.Sections.size())
616         PHeader.p_offset = UINT32_MAX;
617       else
618         PHeader.p_offset = 0;
619 
620       // Find the minimum offset for the program header.
621       for (Elf_Shdr *SHeader : Sections)
622         PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
623     }
624 
625     // Find the maximum offset of the end of a section in order to set p_filesz,
626     // if not set explicitly.
627     if (YamlPhdr.FileSize) {
628       PHeader.p_filesz = *YamlPhdr.FileSize;
629     } else {
630       PHeader.p_filesz = 0;
631       for (Elf_Shdr *SHeader : Sections) {
632         uint64_t EndOfSection;
633         if (SHeader->sh_type == llvm::ELF::SHT_NOBITS)
634           EndOfSection = SHeader->sh_offset;
635         else
636           EndOfSection = SHeader->sh_offset + SHeader->sh_size;
637         uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
638         EndOfSegment = std::max(EndOfSegment, EndOfSection);
639         PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
640       }
641     }
642 
643     // If not set explicitly, find the memory size by adding the size of
644     // sections at the end of the segment. These should be empty (size of zero)
645     // and NOBITS sections.
646     if (YamlPhdr.MemSize) {
647       PHeader.p_memsz = *YamlPhdr.MemSize;
648     } else {
649       PHeader.p_memsz = PHeader.p_filesz;
650       for (Elf_Shdr *SHeader : Sections)
651         if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
652           PHeader.p_memsz += SHeader->sh_size;
653     }
654 
655     // Set the alignment of the segment to be the same as the maximum alignment
656     // of the sections with the same offset so that by default the segment
657     // has a valid and sensible alignment.
658     if (YamlPhdr.Align) {
659       PHeader.p_align = *YamlPhdr.Align;
660     } else {
661       PHeader.p_align = 1;
662       for (Elf_Shdr *SHeader : Sections)
663         if (SHeader->sh_offset == PHeader.p_offset)
664           PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
665     }
666   }
667 }
668 
669 template <class ELFT>
670 bool ELFState<ELFT>::writeSectionContent(
671     Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
672     ContiguousBlobAccumulator &CBA) {
673   raw_ostream &OS =
674       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
675   SHeader.sh_size = writeRawSectionData(OS, Section);
676 
677   if (Section.EntSize)
678     SHeader.sh_entsize = *Section.EntSize;
679   else if (Section.Type == llvm::ELF::SHT_RELR)
680     SHeader.sh_entsize = sizeof(Elf_Relr);
681   else
682     SHeader.sh_entsize = 0;
683 
684   if (Section.Info)
685     SHeader.sh_info = *Section.Info;
686 
687   return true;
688 }
689 
690 static bool isMips64EL(const ELFYAML::Object &Doc) {
691   return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
692          Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
693          Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
694 }
695 
696 template <class ELFT>
697 bool ELFState<ELFT>::writeSectionContent(
698     Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
699     ContiguousBlobAccumulator &CBA) {
700   assert((Section.Type == llvm::ELF::SHT_REL ||
701           Section.Type == llvm::ELF::SHT_RELA) &&
702          "Section type is not SHT_REL nor SHT_RELA");
703 
704   bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
705   SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
706   SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
707 
708   // For relocation section set link to .symtab by default.
709   if (Section.Link.empty())
710     SHeader.sh_link = SN2I.get(".symtab");
711 
712   unsigned Index = 0;
713   if (!Section.RelocatableSec.empty() &&
714       !convertSectionIndex(SN2I, Section.Name, Section.RelocatableSec, Index))
715     return false;
716   SHeader.sh_info = Index;
717 
718   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
719 
720   for (const auto &Rel : Section.Relocations) {
721     unsigned SymIdx = 0;
722     // If a relocation references a symbol, try to look one up in the symbol
723     // table. If it is not there, treat the value as a symbol index.
724     if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
725         !to_integer(*Rel.Symbol, SymIdx)) {
726       WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
727                          << "' at YAML section '" << Section.Name << "'.\n";
728       return false;
729     }
730 
731     if (IsRela) {
732       Elf_Rela REntry;
733       zero(REntry);
734       REntry.r_offset = Rel.Offset;
735       REntry.r_addend = Rel.Addend;
736       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
737       OS.write((const char *)&REntry, sizeof(REntry));
738     } else {
739       Elf_Rel REntry;
740       zero(REntry);
741       REntry.r_offset = Rel.Offset;
742       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
743       OS.write((const char *)&REntry, sizeof(REntry));
744     }
745   }
746   return true;
747 }
748 
749 template <class ELFT>
750 bool ELFState<ELFT>::writeSectionContent(
751     Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
752     ContiguousBlobAccumulator &CBA) {
753   raw_ostream &OS =
754       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
755 
756   for (uint32_t E : Shndx.Entries)
757     support::endian::write<uint32_t>(OS, E, ELFT::TargetEndianness);
758 
759   SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
760   SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize;
761   return true;
762 }
763 
764 template <class ELFT>
765 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
766                                          const ELFYAML::Group &Section,
767                                          ContiguousBlobAccumulator &CBA) {
768   assert(Section.Type == llvm::ELF::SHT_GROUP &&
769          "Section type is not SHT_GROUP");
770 
771   SHeader.sh_entsize = 4;
772   SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
773 
774   unsigned SymIdx;
775   if (!SymN2I.lookup(Section.Signature, SymIdx) &&
776       !to_integer(Section.Signature, SymIdx)) {
777     WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
778                        << "' at YAML section '" << Section.Name << "'.\n";
779     return false;
780   }
781   SHeader.sh_info = SymIdx;
782 
783   raw_ostream &OS =
784       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
785 
786   for (const ELFYAML::SectionOrType &Member : Section.Members) {
787     unsigned int SectionIndex = 0;
788     if (Member.sectionNameOrType == "GRP_COMDAT")
789       SectionIndex = llvm::ELF::GRP_COMDAT;
790     else if (!convertSectionIndex(SN2I, Section.Name, Member.sectionNameOrType,
791                                   SectionIndex))
792       return false;
793     support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
794   }
795   return true;
796 }
797 
798 template <class ELFT>
799 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
800                                          const ELFYAML::SymverSection &Section,
801                                          ContiguousBlobAccumulator &CBA) {
802   raw_ostream &OS =
803       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
804   for (uint16_t Version : Section.Entries)
805     support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
806 
807   SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
808   SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
809   return true;
810 }
811 
812 template <class ELFT>
813 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
814                                          const ELFYAML::VerdefSection &Section,
815                                          ContiguousBlobAccumulator &CBA) {
816   typedef typename ELFT::Verdef Elf_Verdef;
817   typedef typename ELFT::Verdaux Elf_Verdaux;
818   raw_ostream &OS =
819       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
820 
821   uint64_t AuxCnt = 0;
822   for (size_t I = 0; I < Section.Entries.size(); ++I) {
823     const ELFYAML::VerdefEntry &E = Section.Entries[I];
824 
825     Elf_Verdef VerDef;
826     VerDef.vd_version = E.Version;
827     VerDef.vd_flags = E.Flags;
828     VerDef.vd_ndx = E.VersionNdx;
829     VerDef.vd_hash = E.Hash;
830     VerDef.vd_aux = sizeof(Elf_Verdef);
831     VerDef.vd_cnt = E.VerNames.size();
832     if (I == Section.Entries.size() - 1)
833       VerDef.vd_next = 0;
834     else
835       VerDef.vd_next =
836           sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
837     OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
838 
839     for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
840       Elf_Verdaux VernAux;
841       VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
842       if (J == E.VerNames.size() - 1)
843         VernAux.vda_next = 0;
844       else
845         VernAux.vda_next = sizeof(Elf_Verdaux);
846       OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
847     }
848   }
849 
850   SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) +
851                     AuxCnt * sizeof(Elf_Verdaux);
852   SHeader.sh_info = Section.Info;
853 
854   return true;
855 }
856 
857 template <class ELFT>
858 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
859                                          const ELFYAML::VerneedSection &Section,
860                                          ContiguousBlobAccumulator &CBA) {
861   typedef typename ELFT::Verneed Elf_Verneed;
862   typedef typename ELFT::Vernaux Elf_Vernaux;
863 
864   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
865 
866   uint64_t AuxCnt = 0;
867   for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
868     const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
869 
870     Elf_Verneed VerNeed;
871     VerNeed.vn_version = VE.Version;
872     VerNeed.vn_file = DotDynstr.getOffset(VE.File);
873     if (I == Section.VerneedV.size() - 1)
874       VerNeed.vn_next = 0;
875     else
876       VerNeed.vn_next =
877           sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
878     VerNeed.vn_cnt = VE.AuxV.size();
879     VerNeed.vn_aux = sizeof(Elf_Verneed);
880     OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
881 
882     for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
883       const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
884 
885       Elf_Vernaux VernAux;
886       VernAux.vna_hash = VAuxE.Hash;
887       VernAux.vna_flags = VAuxE.Flags;
888       VernAux.vna_other = VAuxE.Other;
889       VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
890       if (J == VE.AuxV.size() - 1)
891         VernAux.vna_next = 0;
892       else
893         VernAux.vna_next = sizeof(Elf_Vernaux);
894       OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
895     }
896   }
897 
898   SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) +
899                     AuxCnt * sizeof(Elf_Vernaux);
900   SHeader.sh_info = Section.Info;
901 
902   return true;
903 }
904 
905 template <class ELFT>
906 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
907                                          const ELFYAML::MipsABIFlags &Section,
908                                          ContiguousBlobAccumulator &CBA) {
909   assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
910          "Section type is not SHT_MIPS_ABIFLAGS");
911 
912   object::Elf_Mips_ABIFlags<ELFT> Flags;
913   zero(Flags);
914   SHeader.sh_entsize = sizeof(Flags);
915   SHeader.sh_size = SHeader.sh_entsize;
916 
917   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
918   Flags.version = Section.Version;
919   Flags.isa_level = Section.ISALevel;
920   Flags.isa_rev = Section.ISARevision;
921   Flags.gpr_size = Section.GPRSize;
922   Flags.cpr1_size = Section.CPR1Size;
923   Flags.cpr2_size = Section.CPR2Size;
924   Flags.fp_abi = Section.FpABI;
925   Flags.isa_ext = Section.ISAExtension;
926   Flags.ases = Section.ASEs;
927   Flags.flags1 = Section.Flags1;
928   Flags.flags2 = Section.Flags2;
929   OS.write((const char *)&Flags, sizeof(Flags));
930 
931   return true;
932 }
933 
934 template <class ELFT>
935 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
936                                          const ELFYAML::DynamicSection &Section,
937                                          ContiguousBlobAccumulator &CBA) {
938   typedef typename ELFT::uint uintX_t;
939 
940   assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
941          "Section type is not SHT_DYNAMIC");
942 
943   if (!Section.Entries.empty() && Section.Content) {
944     WithColor::error()
945         << "Cannot specify both raw content and explicit entries "
946            "for dynamic section '"
947         << Section.Name << "'.\n";
948     return false;
949   }
950 
951   if (Section.Content)
952     SHeader.sh_size = Section.Content->binary_size();
953   else
954     SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
955   if (Section.EntSize)
956     SHeader.sh_entsize = *Section.EntSize;
957   else
958     SHeader.sh_entsize = sizeof(Elf_Dyn);
959 
960   raw_ostream &OS =
961       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
962   for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
963     support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
964     support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
965   }
966   if (Section.Content)
967     Section.Content->writeAsBinary(OS);
968 
969   return true;
970 }
971 
972 template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
973   for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) {
974     StringRef Name = Doc.Sections[I]->Name;
975     if (Name.empty())
976       continue;
977 
978     DotShStrtab.add(dropUniqueSuffix(Name));
979     if (!SN2I.addName(Name, I)) {
980       WithColor::error() << "Repeated section name: '" << Name
981                          << "' at YAML section number " << I << ".\n";
982       return false;
983     }
984   }
985 
986   DotShStrtab.finalize();
987   return true;
988 }
989 
990 template <class ELFT>
991 bool ELFState<ELFT>::buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols) {
992   bool GlobalSymbolSeen = false;
993   std::size_t I = 0;
994   for (const auto &Sym : Symbols) {
995     ++I;
996 
997     StringRef Name = Sym.Name;
998     if (Sym.Binding.value == ELF::STB_LOCAL && GlobalSymbolSeen) {
999       WithColor::error() << "Local symbol '" + Name +
1000                                 "' after global in Symbols list.\n";
1001       return false;
1002     }
1003     if (Sym.Binding.value != ELF::STB_LOCAL)
1004       GlobalSymbolSeen = true;
1005 
1006     if (!Name.empty() && !SymN2I.addName(Name, I)) {
1007       WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
1008       return false;
1009     }
1010   }
1011   return true;
1012 }
1013 
1014 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
1015   // Add the regular symbol names to .strtab section.
1016   for (const ELFYAML::Symbol &Sym : Doc.Symbols)
1017     DotStrtab.add(dropUniqueSuffix(Sym.Name));
1018   DotStrtab.finalize();
1019 
1020   // Add the dynamic symbol names to .dynstr section.
1021   for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols)
1022     DotDynstr.add(dropUniqueSuffix(Sym.Name));
1023 
1024   // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1025   // add strings to .dynstr section.
1026   for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) {
1027     if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
1028       for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) {
1029         DotDynstr.add(VE.File);
1030         for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1031           DotDynstr.add(Aux.Name);
1032       }
1033     } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) {
1034       for (const ELFYAML::VerdefEntry &E : VerDef->Entries)
1035         for (StringRef Name : E.VerNames)
1036           DotDynstr.add(Name);
1037     }
1038   }
1039 
1040   DotDynstr.finalize();
1041 }
1042 
1043 template <class ELFT>
1044 int ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
1045   ELFState<ELFT> State(Doc);
1046 
1047   // Finalize .strtab and .dynstr sections. We do that early because want to
1048   // finalize the string table builders before writing the content of the
1049   // sections that might want to use them.
1050   State.finalizeStrings();
1051 
1052   if (!State.buildSectionIndex())
1053     return 1;
1054 
1055   if (!State.buildSymbolIndex(Doc.Symbols))
1056     return 1;
1057 
1058   Elf_Ehdr Header;
1059   State.initELFHeader(Header);
1060 
1061   // TODO: Flesh out section header support.
1062 
1063   std::vector<Elf_Phdr> PHeaders;
1064   State.initProgramHeaders(PHeaders);
1065 
1066   // XXX: This offset is tightly coupled with the order that we write
1067   // things to `OS`.
1068   const size_t SectionContentBeginOffset = Header.e_ehsize +
1069                                            Header.e_phentsize * Header.e_phnum +
1070                                            Header.e_shentsize * Header.e_shnum;
1071   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
1072 
1073   std::vector<Elf_Shdr> SHeaders;
1074   if (!State.initSectionHeaders(State, SHeaders, CBA))
1075     return 1;
1076 
1077   // Now we can decide segment offsets
1078   State.setProgramHeaderLayout(PHeaders, SHeaders);
1079 
1080   OS.write((const char *)&Header, sizeof(Header));
1081   writeArrayData(OS, makeArrayRef(PHeaders));
1082   writeArrayData(OS, makeArrayRef(SHeaders));
1083   CBA.writeBlobToStream(OS);
1084   return 0;
1085 }
1086 
1087 namespace llvm {
1088 namespace yaml {
1089 
1090 int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
1091   bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1092   bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1093   if (Is64Bit) {
1094     if (IsLE)
1095       return ELFState<object::ELF64LE>::writeELF(Out, Doc);
1096     return ELFState<object::ELF64BE>::writeELF(Out, Doc);
1097   }
1098   if (IsLE)
1099     return ELFState<object::ELF32LE>::writeELF(Out, Doc);
1100   return ELFState<object::ELF32BE>::writeELF(Out, Doc);
1101 }
1102 
1103 } // namespace yaml
1104 } // namespace llvm
1105