LLVM  8.0.1
Binary.cpp
Go to the documentation of this file.
1 //===- Binary.cpp - A generic binary file ---------------------------------===//
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"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/Error.h"
20 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorOr.h"
27 #include <algorithm>
28 #include <memory>
29 #include <system_error>
30 
31 using namespace llvm;
32 using namespace object;
33 
34 Binary::~Binary() = default;
35 
37  : TypeID(Type), Data(Source) {}
38 
39 StringRef Binary::getData() const { return Data.getBuffer(); }
40 
42 
44 
48 
49  switch (Type) {
51  return Archive::create(Buffer);
52  case file_magic::elf:
73  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
75  return MachOUniversalBinary::create(Buffer);
78  case file_magic::pdb:
79  // PDB does not support the Binary interface.
83  // Unrecognized object file format.
85  }
86  llvm_unreachable("Unexpected Binary File Type");
87 }
88 
91  MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
92  /*RequiresNullTerminator=*/false);
93  if (std::error_code EC = FileOrErr.getError())
94  return errorCodeToError(EC);
95  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
96 
98  createBinary(Buffer->getMemBufferRef());
99  if (!BinOrErr)
100  return BinOrErr.takeError();
101  std::unique_ptr<Binary> &Bin = BinOrErr.get();
102 
103  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
104 }
Represents either an error or a value T.
Definition: ErrorOr.h:57
Mach-O Object file.
Definition: Magic.h:31
LLVMContext & Context
ELF core image.
Definition: Magic.h:30
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:29
StringRef getFileName() const
Definition: Binary.cpp:41
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context)
Windows compiled resource file (.res)
Definition: Magic.h:47
Error takeError()
Take ownership of the stored error.
Definition: Error.h:553
Mach-O Bundle file.
Definition: Magic.h:38
Mach-O Preloaded Executable.
Definition: Magic.h:35
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
Bitcode file.
Definition: Magic.h:24
StringRef getBuffer() const
Definition: MemoryBuffer.h:273
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:35
The Mach-O dynamic linker.
Definition: Magic.h:37
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:55
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ELF Relocatable object file.
Definition: Magic.h:27
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
COFF import library.
Definition: Magic.h:45
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
Mach-O universal binary.
Definition: Magic.h:42
Mach-O Core File.
Definition: Magic.h:34
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:531
Mach-O dSYM companion file.
Definition: Magic.h:40
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:88
std::error_code getError() const
Definition: ErrorOr.h:160
PECOFF executable file.
Definition: Magic.h:46
COFF object file.
Definition: Magic.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Mach-O kext bundle file.
Definition: Magic.h:41
ar style archive file
Definition: Magic.h:25
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
reference get()
Returns a reference to the stored T value.
Definition: Error.h:533
ELF Unknown type.
Definition: Magic.h:26
ELF Executable image.
Definition: Magic.h:28
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:275
WebAssembly Object file.
Definition: Magic.h:48
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Provides ErrorOr<T> smart pointer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Mach-O Executable.
Definition: Magic.h:32
Unrecognized file.
Definition: Magic.h:23
StringRef getData() const
Definition: Binary.cpp:39
Windows PDB debug info file.
Definition: Magic.h:49
reference get()
Definition: ErrorOr.h:157
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:21