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