LLVM  3.7.0
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"
23 #include "llvm/Object/ObjectFile.h"
24 
25 using namespace llvm;
26 using namespace object;
27 
29 
30 Binary::Binary(unsigned int Type, MemoryBufferRef Source)
31  : TypeID(Type), Data(Source) {}
32 
33 StringRef Binary::getData() const { return Data.getBuffer(); }
34 
36 
38 
40  LLVMContext *Context) {
42 
43  switch (Type) {
45  return Archive::create(Buffer);
66  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
68  return MachOUniversalBinary::create(Buffer);
71  // Unrecognized object file format.
73  }
74  llvm_unreachable("Unexpected Binary File Type");
75 }
76 
80  if (std::error_code EC = FileOrErr.getError())
81  return EC;
82  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
83 
85  createBinary(Buffer->getMemBufferRef());
86  if (std::error_code EC = BinOrErr.getError())
87  return EC;
88  std::unique_ptr<Binary> &Bin = BinOrErr.get();
89 
90  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
91 }
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
ELF Relocatable object file.
Definition: FileSystem.h:230
virtual ~Binary()
Definition: Binary.cpp:28
static ErrorOr< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
StringRef getData() const
Definition: Binary.cpp:33
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
ELF dynamically linked shared lib.
Definition: FileSystem.h:232
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:37
Windows compiled resource file (.rc)
Definition: FileSystem.h:249
ar style archive file
Definition: FileSystem.h:228
ErrorOr< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:39
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
static ErrorOr< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
StringRef getFileName() const
Definition: Binary.cpp:35
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:900
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
StringRef getBuffer() const
Definition: MemoryBuffer.h:157
MemoryBufferRef Data
Definition: Binary.h:37
static ErrorOr< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:222
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:224
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:159
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
reference get()
Definition: ErrorOr.h:175