From c44c915372ee453bd63a8b6b3eca586ab6f18545 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Sat, 25 Jun 2011 17:54:29 +0000 Subject: Add Binary class. This is a cleaner parent than ObjectFile. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133869 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/Binary.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/Object/Binary.cpp (limited to 'lib/Object/Binary.cpp') diff --git a/lib/Object/Binary.cpp b/lib/Object/Binary.cpp new file mode 100644 index 0000000..75f5a58 --- /dev/null +++ b/lib/Object/Binary.cpp @@ -0,0 +1,50 @@ +//===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the Binary class. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/Binary.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" + +using namespace llvm; +using namespace object; + +Binary::~Binary() { + delete Data; +} + +Binary::Binary(unsigned int Type, MemoryBuffer *Source) + : TypeID(Type) + , Data(Source) {} + +StringRef Binary::getData() const { + return Data->getBuffer(); +} + +StringRef Binary::getFileName() const { + return Data->getBufferIdentifier(); +} + +error_code object::createBinary(MemoryBuffer *Source, + OwningPtr &Result) { + // We don't support any at the moment. + delete Source; + return object_error::invalid_file_type; +} + +error_code object::createBinary(StringRef Path, OwningPtr &Result) { + OwningPtr File; + if (error_code ec = MemoryBuffer::getFile(Path, File)) + return ec; + return createBinary(File.take(), Result); +} -- cgit v1.1 From 001c9205fca2220480589ec355cb6ec701a37e08 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Sat, 25 Jun 2011 17:54:50 +0000 Subject: Make Binary the parent of ObjectFile and update children to new interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133870 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/Binary.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'lib/Object/Binary.cpp') diff --git a/lib/Object/Binary.cpp b/lib/Object/Binary.cpp index 75f5a58..4b31c75 100644 --- a/lib/Object/Binary.cpp +++ b/lib/Object/Binary.cpp @@ -16,6 +16,10 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" +// Include headers for createBinary. +#include "llvm/Object/ObjectFile.h" +#include "llvm/Object/COFF.h" + using namespace llvm; using namespace object; @@ -37,9 +41,51 @@ StringRef Binary::getFileName() const { error_code object::createBinary(MemoryBuffer *Source, OwningPtr &Result) { - // We don't support any at the moment. - delete Source; - return object_error::invalid_file_type; + OwningPtr scopedSource(Source); + if (!Source) + return make_error_code(errc::invalid_argument); + if (Source->getBufferSize() < 64) + return object_error::invalid_file_type; + sys::LLVMFileType type = sys::IdentifyFileType(Source->getBufferStart(), + static_cast(Source->getBufferSize())); + error_code ec; + switch (type) { + case sys::ELF_Relocatable_FileType: + case sys::ELF_Executable_FileType: + case sys::ELF_SharedObject_FileType: + case sys::ELF_Core_FileType: { + OwningPtr ret( + ObjectFile::createELFObjectFile(scopedSource.take())); + if (!ret) + return object_error::invalid_file_type; + Result.swap(ret); + return object_error::success; + } + case sys::Mach_O_Object_FileType: + case sys::Mach_O_Executable_FileType: + case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: + case sys::Mach_O_Core_FileType: + case sys::Mach_O_PreloadExecutable_FileType: + case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: + case sys::Mach_O_DynamicLinker_FileType: + case sys::Mach_O_Bundle_FileType: + case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: { + OwningPtr ret( + ObjectFile::createMachOObjectFile(scopedSource.take())); + if (!ret) + return object_error::invalid_file_type; + Result.swap(ret); + return object_error::success; + } + case sys::COFF_FileType: { + OwningPtr ret(new COFFObjectFile(scopedSource.take(), ec)); + if (ec) return ec; + Result.swap(ret); + return object_error::success; + } + default: // Unrecognized object file format. + return object_error::invalid_file_type; + } } error_code object::createBinary(StringRef Path, OwningPtr &Result) { -- cgit v1.1