795d54262917c6d9341079e90584d70335c73560
[oota-llvm.git] / tools / yaml2obj / yaml2elf.cpp
1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief The ELF component of yaml2obj.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "yaml2obj.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/MC/StringTableBuilder.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #include "llvm/Object/ELFYAML.h"
20 #include "llvm/Support/ELF.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 using namespace llvm;
26
27 // This class is used to build up a contiguous binary blob while keeping
28 // track of an offset in the output (which notionally begins at
29 // `InitialOffset`).
30 namespace {
31 class ContiguousBlobAccumulator {
32   const uint64_t InitialOffset;
33   SmallVector<char, 128> Buf;
34   raw_svector_ostream OS;
35
36   /// \returns The new offset.
37   uint64_t padToAlignment(unsigned Align) {
38     uint64_t CurrentOffset = InitialOffset + OS.tell();
39     uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
40     for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
41       OS.write('\0');
42     return AlignedOffset; // == CurrentOffset;
43   }
44
45 public:
46   ContiguousBlobAccumulator(uint64_t InitialOffset_)
47       : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
48   template <class Integer>
49   raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
50     Offset = padToAlignment(Align);
51     return OS;
52   }
53   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
54 };
55 } // end anonymous namespace
56
57 // Used to keep track of section and symbol names, so that in the YAML file
58 // sections and symbols can be referenced by name instead of by index.
59 namespace {
60 class NameToIdxMap {
61   StringMap<int> Map;
62 public:
63   /// \returns true if name is already present in the map.
64   bool addName(StringRef Name, unsigned i) {
65     return !Map.insert(std::make_pair(Name, (int)i)).second;
66   }
67   /// \returns true if name is not present in the map
68   bool lookup(StringRef Name, unsigned &Idx) const {
69     StringMap<int>::const_iterator I = Map.find(Name);
70     if (I == Map.end())
71       return true;
72     Idx = I->getValue();
73     return false;
74   }
75 };
76 } // end anonymous namespace
77
78 template <class T>
79 static size_t arrayDataSize(ArrayRef<T> A) {
80   return A.size() * sizeof(T);
81 }
82
83 template <class T>
84 static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
85   OS.write((const char *)A.data(), arrayDataSize(A));
86 }
87
88 template <class T>
89 static void zero(T &Obj) {
90   memset(&Obj, 0, sizeof(Obj));
91 }
92
93 namespace {
94 /// \brief "Single point of truth" for the ELF file construction.
95 /// TODO: This class still has a ways to go before it is truly a "single
96 /// point of truth".
97 template <class ELFT>
98 class ELFState {
99   typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
100   typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
101   typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
102   typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
103   typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
104
105   /// \brief The future ".strtab" section.
106   StringTableBuilder DotStrtab;
107
108   /// \brief The future ".shstrtab" section.
109   StringTableBuilder DotShStrtab;
110
111   NameToIdxMap SN2I;
112   NameToIdxMap SymN2I;
113   const ELFYAML::Object &Doc;
114
115   bool buildSectionIndex();
116   bool buildSymbolIndex(std::size_t &StartIndex,
117                         const std::vector<ELFYAML::Symbol> &Symbols);
118   void initELFHeader(Elf_Ehdr &Header);
119   bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
120                           ContiguousBlobAccumulator &CBA);
121   void initSymtabSectionHeader(Elf_Shdr &SHeader,
122                                ContiguousBlobAccumulator &CBA);
123   void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
124                                StringTableBuilder &STB,
125                                ContiguousBlobAccumulator &CBA);
126   void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
127                   std::vector<Elf_Sym> &Syms, unsigned SymbolBinding);
128   void writeSectionContent(Elf_Shdr &SHeader,
129                            const ELFYAML::RawContentSection &Section,
130                            ContiguousBlobAccumulator &CBA);
131   bool writeSectionContent(Elf_Shdr &SHeader,
132                            const ELFYAML::RelocationSection &Section,
133                            ContiguousBlobAccumulator &CBA);
134   bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
135                            ContiguousBlobAccumulator &CBA);
136   bool writeSectionContent(Elf_Shdr &SHeader,
137                            const ELFYAML::MipsABIFlags &Section,
138                            ContiguousBlobAccumulator &CBA);
139
140   // - SHT_NULL entry (placed first, i.e. 0'th entry)
141   // - symbol table (.symtab) (placed third to last)
142   // - string table (.strtab) (placed second to last)
143   // - section header string table (.shstrtab) (placed last)
144   unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
145   unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
146   unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
147   unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
148
149   ELFState(const ELFYAML::Object &D) : Doc(D) {}
150
151 public:
152   static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
153 };
154 } // end anonymous namespace
155
156 template <class ELFT>
157 void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
158   using namespace llvm::ELF;
159   zero(Header);
160   Header.e_ident[EI_MAG0] = 0x7f;
161   Header.e_ident[EI_MAG1] = 'E';
162   Header.e_ident[EI_MAG2] = 'L';
163   Header.e_ident[EI_MAG3] = 'F';
164   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
165   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
166   Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
167   Header.e_ident[EI_VERSION] = EV_CURRENT;
168   Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
169   Header.e_ident[EI_ABIVERSION] = 0;
170   Header.e_type = Doc.Header.Type;
171   Header.e_machine = Doc.Header.Machine;
172   Header.e_version = EV_CURRENT;
173   Header.e_entry = Doc.Header.Entry;
174   Header.e_flags = Doc.Header.Flags;
175   Header.e_ehsize = sizeof(Elf_Ehdr);
176   Header.e_shentsize = sizeof(Elf_Shdr);
177   // Immediately following the ELF header.
178   Header.e_shoff = sizeof(Header);
179   Header.e_shnum = getSectionCount();
180   Header.e_shstrndx = getDotShStrTabSecNo();
181 }
182
183 template <class ELFT>
184 bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
185                                         ContiguousBlobAccumulator &CBA) {
186   // Ensure SHN_UNDEF entry is present. An all-zero section header is a
187   // valid SHN_UNDEF entry since SHT_NULL == 0.
188   Elf_Shdr SHeader;
189   zero(SHeader);
190   SHeaders.push_back(SHeader);
191
192   for (const auto &Sec : Doc.Sections)
193     DotShStrtab.add(Sec->Name);
194   DotShStrtab.finalize(StringTableBuilder::ELF);
195
196   for (const auto &Sec : Doc.Sections) {
197     zero(SHeader);
198     SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
199     SHeader.sh_type = Sec->Type;
200     SHeader.sh_flags = Sec->Flags;
201     SHeader.sh_addr = Sec->Address;
202     SHeader.sh_addralign = Sec->AddressAlign;
203
204     if (!Sec->Link.empty()) {
205       unsigned Index;
206       if (SN2I.lookup(Sec->Link, Index)) {
207         errs() << "error: Unknown section referenced: '" << Sec->Link
208                << "' at YAML section '" << Sec->Name << "'.\n";
209         return false;
210       }
211       SHeader.sh_link = Index;
212     }
213
214     if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
215       writeSectionContent(SHeader, *S, CBA);
216     else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
217       if (S->Link.empty())
218         // For relocation section set link to .symtab by default.
219         SHeader.sh_link = getDotSymTabSecNo();
220
221       unsigned Index;
222       if (SN2I.lookup(S->Info, Index)) {
223         errs() << "error: Unknown section referenced: '" << S->Info
224                << "' at YAML section '" << S->Name << "'.\n";
225         return false;
226       }
227       SHeader.sh_info = Index;
228
229       if (!writeSectionContent(SHeader, *S, CBA))
230         return false;
231     } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
232       unsigned SymIdx;
233       if (SymN2I.lookup(S->Info, SymIdx)) {
234         errs() << "error: Unknown symbol referenced: '" << S->Info
235                << "' at YAML section '" << S->Name << "'.\n";
236         return false;
237       }
238       SHeader.sh_info = SymIdx;
239       if (!writeSectionContent(SHeader, *S, CBA))
240         return false;
241     } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
242       if (!writeSectionContent(SHeader, *S, CBA))
243         return false;
244     } else if (isa<ELFYAML::NoBitsSection>(Sec.get())) {
245       // SHT_NOBITS section does not have content so nothing to do here.
246     } else
247       llvm_unreachable("Unknown section type");
248
249     SHeaders.push_back(SHeader);
250   }
251   return true;
252 }
253
254 template <class ELFT>
255 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
256                                              ContiguousBlobAccumulator &CBA) {
257   zero(SHeader);
258   SHeader.sh_name = DotShStrtab.getOffset(".symtab");
259   SHeader.sh_type = ELF::SHT_SYMTAB;
260   SHeader.sh_link = getDotStrTabSecNo();
261   // One greater than symbol table index of the last local symbol.
262   SHeader.sh_info = Doc.Symbols.Local.size() + 1;
263   SHeader.sh_entsize = sizeof(Elf_Sym);
264
265   std::vector<Elf_Sym> Syms;
266   {
267     // Ensure STN_UNDEF is present
268     Elf_Sym Sym;
269     zero(Sym);
270     Syms.push_back(Sym);
271   }
272
273   // Add symbol names to .strtab.
274   for (const auto &Sym : Doc.Symbols.Local)
275     DotStrtab.add(Sym.Name);
276   for (const auto &Sym : Doc.Symbols.Global)
277     DotStrtab.add(Sym.Name);
278   for (const auto &Sym : Doc.Symbols.Weak)
279     DotStrtab.add(Sym.Name);
280   DotStrtab.finalize(StringTableBuilder::ELF);
281
282   addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
283   addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
284   addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
285
286   writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
287                  makeArrayRef(Syms));
288   SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
289 }
290
291 template <class ELFT>
292 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
293                                              StringTableBuilder &STB,
294                                              ContiguousBlobAccumulator &CBA) {
295   zero(SHeader);
296   SHeader.sh_name = DotShStrtab.getOffset(Name);
297   SHeader.sh_type = ELF::SHT_STRTAB;
298   CBA.getOSAndAlignedOffset(SHeader.sh_offset) << STB.data();
299   SHeader.sh_size = STB.data().size();
300   SHeader.sh_addralign = 1;
301 }
302
303 template <class ELFT>
304 void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
305                                 std::vector<Elf_Sym> &Syms,
306                                 unsigned SymbolBinding) {
307   for (const auto &Sym : Symbols) {
308     Elf_Sym Symbol;
309     zero(Symbol);
310     if (!Sym.Name.empty())
311       Symbol.st_name = DotStrtab.getOffset(Sym.Name);
312     Symbol.setBindingAndType(SymbolBinding, Sym.Type);
313     if (!Sym.Section.empty()) {
314       unsigned Index;
315       if (SN2I.lookup(Sym.Section, Index)) {
316         errs() << "error: Unknown section referenced: '" << Sym.Section
317                << "' by YAML symbol " << Sym.Name << ".\n";
318         exit(1);
319       }
320       Symbol.st_shndx = Index;
321     } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
322     Symbol.st_value = Sym.Value;
323     Symbol.st_other = Sym.Other;
324     Symbol.st_size = Sym.Size;
325     Syms.push_back(Symbol);
326   }
327 }
328
329 template <class ELFT>
330 void
331 ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
332                                     const ELFYAML::RawContentSection &Section,
333                                     ContiguousBlobAccumulator &CBA) {
334   assert(Section.Size >= Section.Content.binary_size() &&
335          "Section size and section content are inconsistent");
336   raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
337   Section.Content.writeAsBinary(OS);
338   for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
339     OS.write(0);
340   SHeader.sh_entsize = 0;
341   SHeader.sh_size = Section.Size;
342 }
343
344 static bool isMips64EL(const ELFYAML::Object &Doc) {
345   return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
346          Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
347          Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
348 }
349
350 template <class ELFT>
351 bool
352 ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
353                                     const ELFYAML::RelocationSection &Section,
354                                     ContiguousBlobAccumulator &CBA) {
355   assert((Section.Type == llvm::ELF::SHT_REL ||
356           Section.Type == llvm::ELF::SHT_RELA) &&
357          "Section type is not SHT_REL nor SHT_RELA");
358
359   bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
360   SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
361   SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
362
363   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
364
365   for (const auto &Rel : Section.Relocations) {
366     unsigned SymIdx = 0;
367     // Some special relocation, R_ARM_v4BX for instance, does not have
368     // an external reference.  So it ignores the return value of lookup()
369     // here.
370     SymN2I.lookup(Rel.Symbol, SymIdx);
371
372     if (IsRela) {
373       Elf_Rela REntry;
374       zero(REntry);
375       REntry.r_offset = Rel.Offset;
376       REntry.r_addend = Rel.Addend;
377       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
378       OS.write((const char *)&REntry, sizeof(REntry));
379     } else {
380       Elf_Rel REntry;
381       zero(REntry);
382       REntry.r_offset = Rel.Offset;
383       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
384       OS.write((const char *)&REntry, sizeof(REntry));
385     }
386   }
387   return true;
388 }
389
390 template <class ELFT>
391 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
392                                          const ELFYAML::Group &Section,
393                                          ContiguousBlobAccumulator &CBA) {
394   typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
395   assert(Section.Type == llvm::ELF::SHT_GROUP &&
396          "Section type is not SHT_GROUP");
397
398   SHeader.sh_entsize = sizeof(Elf_Word);
399   SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
400
401   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
402
403   for (auto member : Section.Members) {
404     Elf_Word SIdx;
405     unsigned int sectionIndex = 0;
406     if (member.sectionNameOrType == "GRP_COMDAT")
407       sectionIndex = llvm::ELF::GRP_COMDAT;
408     else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
409       errs() << "error: Unknown section referenced: '"
410              << member.sectionNameOrType << "' at YAML section' "
411              << Section.Name << "\n";
412       return false;
413     }
414     SIdx = sectionIndex;
415     OS.write((const char *)&SIdx, sizeof(SIdx));
416   }
417   return true;
418 }
419
420 template <class ELFT>
421 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
422                                          const ELFYAML::MipsABIFlags &Section,
423                                          ContiguousBlobAccumulator &CBA) {
424   assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
425          "Section type is not SHT_MIPS_ABIFLAGS");
426
427   object::Elf_Mips_ABIFlags<ELFT> Flags;
428   zero(Flags);
429   SHeader.sh_entsize = sizeof(Flags);
430   SHeader.sh_size = SHeader.sh_entsize;
431
432   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
433   Flags.version = Section.Version;
434   Flags.isa_level = Section.ISALevel;
435   Flags.isa_rev = Section.ISARevision;
436   Flags.gpr_size = Section.GPRSize;
437   Flags.cpr1_size = Section.CPR1Size;
438   Flags.cpr2_size = Section.CPR2Size;
439   Flags.fp_abi = Section.FpABI;
440   Flags.isa_ext = Section.ISAExtension;
441   Flags.ases = Section.ASEs;
442   Flags.flags1 = Section.Flags1;
443   Flags.flags2 = Section.Flags2;
444   OS.write((const char *)&Flags, sizeof(Flags));
445
446   return true;
447 }
448
449 template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
450   SN2I.addName(".symtab", getDotSymTabSecNo());
451   SN2I.addName(".strtab", getDotStrTabSecNo());
452   SN2I.addName(".shstrtab", getDotShStrTabSecNo());
453
454   for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
455     StringRef Name = Doc.Sections[i]->Name;
456     if (Name.empty())
457       continue;
458     // "+ 1" to take into account the SHT_NULL entry.
459     if (SN2I.addName(Name, i + 1)) {
460       errs() << "error: Repeated section name: '" << Name
461              << "' at YAML section number " << i << ".\n";
462       return false;
463     }
464   }
465   return true;
466 }
467
468 template <class ELFT>
469 bool
470 ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
471                                  const std::vector<ELFYAML::Symbol> &Symbols) {
472   for (const auto &Sym : Symbols) {
473     ++StartIndex;
474     if (Sym.Name.empty())
475       continue;
476     if (SymN2I.addName(Sym.Name, StartIndex)) {
477       errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n";
478       return false;
479     }
480   }
481   return true;
482 }
483
484 template <class ELFT>
485 int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
486   ELFState<ELFT> State(Doc);
487   if (!State.buildSectionIndex())
488     return 1;
489
490   std::size_t StartSymIndex = 0;
491   if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
492       !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
493       !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
494     return 1;
495
496   Elf_Ehdr Header;
497   State.initELFHeader(Header);
498
499   // TODO: Flesh out section header support.
500   // TODO: Program headers.
501
502   // XXX: This offset is tightly coupled with the order that we write
503   // things to `OS`.
504   const size_t SectionContentBeginOffset =
505       Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
506   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
507
508   // Doc might not contain .symtab, .strtab and .shstrtab sections,
509   // but we will emit them, so make sure to add them to ShStrTabSHeader.
510   State.DotShStrtab.add(".symtab");
511   State.DotShStrtab.add(".strtab");
512   State.DotShStrtab.add(".shstrtab");
513
514   std::vector<Elf_Shdr> SHeaders;
515   if(!State.initSectionHeaders(SHeaders, CBA))
516     return 1;
517
518   // .symtab section.
519   Elf_Shdr SymtabSHeader;
520   State.initSymtabSectionHeader(SymtabSHeader, CBA);
521   SHeaders.push_back(SymtabSHeader);
522
523   // .strtab string table header.
524   Elf_Shdr DotStrTabSHeader;
525   State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
526                                 CBA);
527   SHeaders.push_back(DotStrTabSHeader);
528
529   // .shstrtab string table header.
530   Elf_Shdr ShStrTabSHeader;
531   State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
532                                 CBA);
533   SHeaders.push_back(ShStrTabSHeader);
534
535   OS.write((const char *)&Header, sizeof(Header));
536   writeArrayData(OS, makeArrayRef(SHeaders));
537   CBA.writeBlobToStream(OS);
538   return 0;
539 }
540
541 static bool is64Bit(const ELFYAML::Object &Doc) {
542   return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
543 }
544
545 static bool isLittleEndian(const ELFYAML::Object &Doc) {
546   return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
547 }
548
549 int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
550   ELFYAML::Object Doc;
551   YIn >> Doc;
552   if (YIn.error()) {
553     errs() << "yaml2obj: Failed to parse YAML file!\n";
554     return 1;
555   }
556   using object::ELFType;
557   typedef ELFType<support::little, true> LE64;
558   typedef ELFType<support::big, true> BE64;
559   typedef ELFType<support::little, false> LE32;
560   typedef ELFType<support::big, false> BE32;
561   if (is64Bit(Doc)) {
562     if (isLittleEndian(Doc))
563       return ELFState<LE64>::writeELF(Out, Doc);
564     else
565       return ELFState<BE64>::writeELF(Out, Doc);
566   } else {
567     if (isLittleEndian(Doc))
568       return ELFState<LE32>::writeELF(Out, Doc);
569     else
570       return ELFState<BE32>::writeELF(Out, Doc);
571   }
572 }