Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / Object / Binary.cpp
blob4b31c7557dd3a4e9699e30fd8706cb1d7938db48
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.
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"
23 using namespace llvm;
24 using namespace object;
26 Binary::~Binary() {
27 delete Data;
30 Binary::Binary(unsigned int Type, MemoryBuffer *Source)
31 : TypeID(Type)
32 , Data(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);
45 if (!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()));
51 error_code ec;
52 switch (type) {
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()));
59 if (!ret)
60 return object_error::invalid_file_type;
61 Result.swap(ret);
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()));
75 if (!ret)
76 return object_error::invalid_file_type;
77 Result.swap(ret);
78 return object_error::success;
80 case sys::COFF_FileType: {
81 OwningPtr<Binary> ret(new COFFObjectFile(scopedSource.take(), ec));
82 if (ec) return ec;
83 Result.swap(ret);
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))
94 return ec;
95 return createBinary(File.take(), Result);