LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Binary.cpp
Go to the documentation of this file.
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"
18 #include "llvm/Support/Path.h"
19 
20 // Include headers for createBinary.
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/ObjectFile.h"
25 
26 using namespace llvm;
27 using namespace object;
28 
30  delete Data;
31 }
32 
33 Binary::Binary(unsigned int Type, MemoryBuffer *Source)
34  : TypeID(Type)
35  , Data(Source) {}
36 
38  return Data->getBuffer();
39 }
40 
42  return Data->getBufferIdentifier();
43 }
44 
46  OwningPtr<Binary> &Result) {
47  OwningPtr<MemoryBuffer> scopedSource(Source);
48  if (!Source)
51  error_code ec;
52  switch (type) {
54  OwningPtr<Binary> ret(new Archive(scopedSource.take(), ec));
55  if (ec) return ec;
56  Result.swap(ret);
57  return object_error::success;
58  }
64  ObjectFile::createELFObjectFile(scopedSource.take()));
65  if (!ret)
67  Result.swap(ret);
68  return object_error::success;
69  }
81  ObjectFile::createMachOObjectFile(scopedSource.take()));
82  if (!ret)
84  Result.swap(ret);
85  return object_error::success;
86  }
88  OwningPtr<Binary> ret(new MachOUniversalBinary(scopedSource.take(), ec));
89  if (ec) return ec;
90  Result.swap(ret);
91  return object_error::success;
92  }
97  ObjectFile::createCOFFObjectFile(scopedSource.take()));
98  if (!ret)
100  Result.swap(ret);
101  return object_error::success;
102  }
106  // Unrecognized object file format.
108  }
109  }
110  llvm_unreachable("Unexpected Binary File Type");
111 }
112 
115  if (error_code ec = MemoryBuffer::getFileOrSTDIN(Path, File))
116  return ec;
117  return createBinary(File.take(), Result);
118 }
static ObjectFile * createELFObjectFile(MemoryBuffer *Object)
StringRef getBuffer() const
Definition: MemoryBuffer.h:55
ELF Relocatable object file.
Definition: FileSystem.h:225
virtual ~Binary()
Definition: Binary.cpp:29
virtual const char * getBufferIdentifier() const
Definition: MemoryBuffer.h:61
error_code make_error_code(object_error e)
Definition: Object/Error.h:38
#define llvm_unreachable(msg)
StringRef getData() const
Definition: Binary.cpp:37
TypeID
Definition: Type.h:53
ELF dynamically linked shared lib.
Definition: FileSystem.h:227
Windows compiled resource file (.rc)
Definition: FileSystem.h:243
static error_code getFileOrSTDIN(StringRef Filename, OwningPtr< MemoryBuffer > &result, int64_t FileSize=-1)
ar style archive file
Definition: FileSystem.h:224
error_code createBinary(MemoryBuffer *Source, OwningPtr< Binary > &Result)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
StringRef getFileName() const
Definition: Binary.cpp:41
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:846
static ObjectFile * createCOFFObjectFile(MemoryBuffer *Object)
MemoryBuffer * Data
Definition: Binary.h:35
void swap(OwningPtr &RHS)
Definition: OwningPtr.h:77
static ObjectFile * createMachOObjectFile(MemoryBuffer *Object)