From 5918b7a03d4d6a52e18f7c102250c9cfd6ae52dd Mon Sep 17 00:00:00 2001 From: Sean Silva Date: Mon, 10 Jun 2013 23:44:15 +0000 Subject: [yaml2obj] Initial ELF support. Currently, only emitting the ELF header is supported (no sections or segments). The ELFYAML code organization is broadly similar to the COFFYAML code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183711 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/yaml2obj/CMakeLists.txt | 1 + tools/yaml2obj/yaml2elf.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++ tools/yaml2obj/yaml2obj.cpp | 6 +++- tools/yaml2obj/yaml2obj.h | 1 + 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tools/yaml2obj/yaml2elf.cpp (limited to 'tools') diff --git a/tools/yaml2obj/CMakeLists.txt b/tools/yaml2obj/CMakeLists.txt index 5cccf9330e..8d9d652246 100644 --- a/tools/yaml2obj/CMakeLists.txt +++ b/tools/yaml2obj/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS object) add_llvm_utility(yaml2obj yaml2obj.cpp yaml2coff.cpp + yaml2elf.cpp ) target_link_libraries(yaml2obj LLVMSupport) diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp new file mode 100644 index 0000000000..d59594236f --- /dev/null +++ b/tools/yaml2obj/yaml2elf.cpp @@ -0,0 +1,78 @@ +//===- yaml2elf - Convert YAML to a ELF object file -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief The ELF component of yaml2obj. +/// +//===----------------------------------------------------------------------===// + +#include "yaml2obj.h" +#include "llvm/Object/ELF.h" +#include "llvm/Object/ELFYAML.h" +#include "llvm/Support/ELF.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/YAMLTraits.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +template +static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { + const ELFYAML::Header &Hdr = Doc.Header; + using namespace llvm::ELF; + using namespace llvm::object; + typename ELFObjectFile::Elf_Ehdr Header; + memset(&Header, 0, sizeof(Header)); + Header.e_ident[EI_MAG0] = 0x7f; + Header.e_ident[EI_MAG1] = 'E'; + Header.e_ident[EI_MAG2] = 'L'; + Header.e_ident[EI_MAG3] = 'F'; + Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; + bool IsLittleEndian = ELFT::TargetEndianness == support::little; + Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; + + Header.e_ident[EI_VERSION] = EV_CURRENT; + + // TODO: Implement ELF_ELFOSABI enum. + Header.e_ident[EI_OSABI] = ELFOSABI_NONE; + // TODO: Implement ELF_ABIVERSION enum. + Header.e_ident[EI_ABIVERSION] = 0; + Header.e_type = Hdr.Type; + Header.e_machine = Hdr.Machine; + Header.e_version = EV_CURRENT; + Header.e_entry = Hdr.Entry; + Header.e_ehsize = sizeof(Header); + + // TODO: Section headers and program headers. + + OS.write((const char *)&Header, sizeof(Header)); +} + +int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) { + yaml::Input YIn(Buf->getBuffer()); + ELFYAML::Object Doc; + YIn >> Doc; + if (YIn.error()) { + errs() << "yaml2obj: Failed to parse YAML file!\n"; + return 1; + } + if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { + if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) + writeELF >(outs(), Doc); + else + writeELF >(outs(), Doc); + } else { + if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) + writeELF >(outs(), Doc); + else + writeELF >(outs(), Doc); + } + + return 0; +} diff --git a/tools/yaml2obj/yaml2obj.cpp b/tools/yaml2obj/yaml2obj.cpp index a9ac78bce4..6d1107c858 100644 --- a/tools/yaml2obj/yaml2obj.cpp +++ b/tools/yaml2obj/yaml2obj.cpp @@ -38,7 +38,8 @@ static cl::opt // them appropriately requires some work in the YAML parser and the YAMLIO // library. enum YAMLObjectFormat { - YOF_COFF + YOF_COFF, + YOF_ELF }; cl::opt Format( @@ -46,6 +47,7 @@ cl::opt Format( cl::desc("Interpret input as this type of object file"), cl::values( clEnumValN(YOF_COFF, "coff", "COFF object file format"), + clEnumValN(YOF_ELF, "elf", "ELF object file format"), clEnumValEnd)); @@ -60,6 +62,8 @@ int main(int argc, char **argv) { return 1; if (Format == YOF_COFF) { return yaml2coff(outs(), Buf.get()); + } else if (Format == YOF_ELF) { + return yaml2elf(outs(), Buf.get()); } else { errs() << "Not yet implemented\n"; return 1; diff --git a/tools/yaml2obj/yaml2obj.h b/tools/yaml2obj/yaml2obj.h index 7197410b26..095435c549 100644 --- a/tools/yaml2obj/yaml2obj.h +++ b/tools/yaml2obj/yaml2obj.h @@ -17,5 +17,6 @@ namespace llvm { class MemoryBuffer; } int yaml2coff(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf); +int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf); #endif -- cgit v1.2.3