1 //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
10 // This file defines the Binary class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/Binary.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/Path.h"
19 // Include headers for createBinary.
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Object/COFF.h"
24 using namespace object
;
30 Binary::Binary(unsigned int Type
, MemoryBuffer
*Source
)
34 StringRef
Binary::getData() const {
35 return Data
->getBuffer();
38 StringRef
Binary::getFileName() const {
39 return Data
->getBufferIdentifier();
42 error_code
object::createBinary(MemoryBuffer
*Source
,
43 OwningPtr
<Binary
> &Result
) {
44 OwningPtr
<MemoryBuffer
> scopedSource(Source
);
46 return make_error_code(errc::invalid_argument
);
47 if (Source
->getBufferSize() < 64)
48 return object_error::invalid_file_type
;
49 sys::LLVMFileType type
= sys::IdentifyFileType(Source
->getBufferStart(),
50 static_cast<unsigned>(Source
->getBufferSize()));
53 case sys::ELF_Relocatable_FileType
:
54 case sys::ELF_Executable_FileType
:
55 case sys::ELF_SharedObject_FileType
:
56 case sys::ELF_Core_FileType
: {
57 OwningPtr
<Binary
> ret(
58 ObjectFile::createELFObjectFile(scopedSource
.take()));
60 return object_error::invalid_file_type
;
62 return object_error::success
;
64 case sys::Mach_O_Object_FileType
:
65 case sys::Mach_O_Executable_FileType
:
66 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType
:
67 case sys::Mach_O_Core_FileType
:
68 case sys::Mach_O_PreloadExecutable_FileType
:
69 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType
:
70 case sys::Mach_O_DynamicLinker_FileType
:
71 case sys::Mach_O_Bundle_FileType
:
72 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType
: {
73 OwningPtr
<Binary
> ret(
74 ObjectFile::createMachOObjectFile(scopedSource
.take()));
76 return object_error::invalid_file_type
;
78 return object_error::success
;
80 case sys::COFF_FileType
: {
81 OwningPtr
<Binary
> ret(new COFFObjectFile(scopedSource
.take(), ec
));
84 return object_error::success
;
86 default: // Unrecognized object file format.
87 return object_error::invalid_file_type
;
91 error_code
object::createBinary(StringRef Path
, OwningPtr
<Binary
> &Result
) {
92 OwningPtr
<MemoryBuffer
> File
;
93 if (error_code ec
= MemoryBuffer::getFile(Path
, File
))
95 return createBinary(File
.take(), Result
);