1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The ELF component of yaml2obj.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Object/ELF.h"
17 #include "llvm/Object/ELFYAML.h"
18 #include "llvm/Support/ELF.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/YAMLTraits.h"
21 #include "llvm/Support/raw_ostream.h"
26 static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
27 const ELFYAML::FileHeader &Hdr = Doc.Header;
28 using namespace llvm::ELF;
29 using namespace llvm::object;
30 typename ELFObjectFile<ELFT>::Elf_Ehdr Header;
31 memset(&Header, 0, sizeof(Header));
32 Header.e_ident[EI_MAG0] = 0x7f;
33 Header.e_ident[EI_MAG1] = 'E';
34 Header.e_ident[EI_MAG2] = 'L';
35 Header.e_ident[EI_MAG3] = 'F';
36 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
37 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
38 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
40 Header.e_ident[EI_VERSION] = EV_CURRENT;
42 // TODO: Implement ELF_ELFOSABI enum.
43 Header.e_ident[EI_OSABI] = ELFOSABI_NONE;
44 // TODO: Implement ELF_ABIVERSION enum.
45 Header.e_ident[EI_ABIVERSION] = 0;
46 Header.e_type = Hdr.Type;
47 Header.e_machine = Hdr.Machine;
48 Header.e_version = EV_CURRENT;
49 Header.e_entry = Hdr.Entry;
50 Header.e_ehsize = sizeof(Header);
52 // TODO: Section headers and program headers.
54 OS.write((const char *)&Header, sizeof(Header));
57 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
58 yaml::Input YIn(Buf->getBuffer());
62 errs() << "yaml2obj: Failed to parse YAML file!\n";
65 if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
66 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
67 writeELF<object::ELFType<support::little, 8, true> >(outs(), Doc);
69 writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc);
71 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
72 writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc);
74 writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc);