Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame^] | 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/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" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | template <class ELFT> |
| 26 | static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
| 27 | const ELFYAML::Header &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; |
| 39 | |
| 40 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
| 41 | |
| 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); |
| 51 | |
| 52 | // TODO: Section headers and program headers. |
| 53 | |
| 54 | OS.write((const char *)&Header, sizeof(Header)); |
| 55 | } |
| 56 | |
| 57 | int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) { |
| 58 | yaml::Input YIn(Buf->getBuffer()); |
| 59 | ELFYAML::Object Doc; |
| 60 | YIn >> Doc; |
| 61 | if (YIn.error()) { |
| 62 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 63 | return 1; |
| 64 | } |
| 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); |
| 68 | else |
| 69 | writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc); |
| 70 | } else { |
| 71 | if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) |
| 72 | writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc); |
| 73 | else |
| 74 | writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc); |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |