Return an ErrorOr<Binary *> from createBinary.
[oota-llvm.git] / lib / Object / Binary.cpp
1 //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===//
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 // This file defines the Binary class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Object/Binary.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Path.h"
19
20 // Include headers for createBinary.
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Object/MachOUniversal.h"
24 #include "llvm/Object/ObjectFile.h"
25
26 using namespace llvm;
27 using namespace object;
28
29 Binary::~Binary() {
30   delete Data;
31 }
32
33 Binary::Binary(unsigned int Type, MemoryBuffer *Source)
34   : TypeID(Type)
35   , Data(Source) {}
36
37 StringRef Binary::getData() const {
38   return Data->getBuffer();
39 }
40
41 StringRef Binary::getFileName() const {
42   return Data->getBufferIdentifier();
43 }
44
45 ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source) {
46   OwningPtr<MemoryBuffer> scopedSource(Source);
47   sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer());
48   error_code EC;
49   switch (type) {
50     case sys::fs::file_magic::archive: {
51       OwningPtr<Binary> Ret(new Archive(scopedSource.take(), EC));
52       if (EC)
53         return EC;
54       return Ret.take();
55     }
56     case sys::fs::file_magic::elf_relocatable:
57     case sys::fs::file_magic::elf_executable:
58     case sys::fs::file_magic::elf_shared_object:
59     case sys::fs::file_magic::elf_core: {
60       OwningPtr<Binary> Ret(
61           ObjectFile::createELFObjectFile(scopedSource.take()));
62       if (!Ret)
63         return object_error::invalid_file_type;
64       return Ret.take();
65     }
66     case sys::fs::file_magic::macho_object:
67     case sys::fs::file_magic::macho_executable:
68     case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
69     case sys::fs::file_magic::macho_core:
70     case sys::fs::file_magic::macho_preload_executable:
71     case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
72     case sys::fs::file_magic::macho_dynamic_linker:
73     case sys::fs::file_magic::macho_bundle:
74     case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
75     case sys::fs::file_magic::macho_dsym_companion: {
76       OwningPtr<Binary> Ret(
77           ObjectFile::createMachOObjectFile(scopedSource.take()));
78       if (!Ret)
79         return object_error::invalid_file_type;
80       return Ret.take();
81     }
82     case sys::fs::file_magic::macho_universal_binary: {
83       OwningPtr<Binary> Ret(new MachOUniversalBinary(scopedSource.take(), EC));
84       if (EC)
85         return EC;
86       return Ret.take();
87     }
88     case sys::fs::file_magic::coff_object:
89     case sys::fs::file_magic::coff_import_library:
90     case sys::fs::file_magic::pecoff_executable: {
91       OwningPtr<Binary> Ret(
92           ObjectFile::createCOFFObjectFile(scopedSource.take()));
93       if (!Ret)
94         return object_error::invalid_file_type;
95       return Ret.take();
96     }
97     case sys::fs::file_magic::unknown:
98     case sys::fs::file_magic::bitcode:
99     case sys::fs::file_magic::windows_resource: {
100       // Unrecognized object file format.
101       return object_error::invalid_file_type;
102     }
103   }
104   llvm_unreachable("Unexpected Binary File Type");
105 }
106
107 ErrorOr<Binary *> object::createBinary(StringRef Path) {
108   OwningPtr<MemoryBuffer> File;
109   if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File))
110     return EC;
111   return createBinary(File.take());
112 }