LLVM  9.0.0svn
Binary.cpp
Go to the documentation of this file.
1 //===- Binary.cpp - A generic binary file ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Binary class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/Binary.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Object/Archive.h"
17 #include "llvm/Object/Error.h"
19 #include "llvm/Object/Minidump.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:
75  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
77  return MachOUniversalBinary::create(Buffer);
80  case file_magic::pdb:
81  // PDB does not support the Binary interface.
85  // Unrecognized object file format.
88  return MinidumpFile::create(Buffer);
89  }
90  llvm_unreachable("Unexpected Binary File Type");
91 }
92 
95  MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
96  /*RequiresNullTerminator=*/false);
97  if (std::error_code EC = FileOrErr.getError())
98  return errorCodeToError(EC);
99  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
100 
102  createBinary(Buffer->getMemBufferRef());
103  if (!BinOrErr)
104  return BinOrErr.takeError();
105  std::unique_ptr<Binary> &Bin = BinOrErr.get();
106 
107  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
108 }
32-bit XCOFF object file
Definition: Magic.h:48
Represents either an error or a value T.
Definition: ErrorOr.h:56
Mach-O Object file.
Definition: Magic.h:30
LLVMContext & Context
ELF core image.
Definition: Magic.h:29
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:28
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:552
Mach-O Bundle file.
Definition: Magic.h:37
Mach-O Preloaded Executable.
Definition: Magic.h:34
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:23
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
The Mach-O dynamic linker.
Definition: Magic.h:36
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
64-bit XCOFF object file
Definition: Magic.h:49
ELF Relocatable object file.
Definition: Magic.h:26
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
Windows minidump file.
Definition: Magic.h:42
COFF import library.
Definition: Magic.h:45
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
Mach-O universal binary.
Definition: Magic.h:41
Mach-O Core File.
Definition: Magic.h:33
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:530
Mach-O dSYM companion file.
Definition: Magic.h:39
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:94
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:64
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
std::error_code getError() const
Definition: ErrorOr.h:159
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:40
ar style archive file
Definition: Magic.h:24
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
ELF Unknown type.
Definition: Magic.h:25
ELF Executable image.
Definition: Magic.h:27
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:274
WebAssembly Object file.
Definition: Magic.h:50
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:48
Mach-O Executable.
Definition: Magic.h:31
Unrecognized file.
Definition: Magic.h:22
StringRef getData() const
Definition: Binary.cpp:39
Windows PDB debug info file.
Definition: Magic.h:51
reference get()
Definition: ErrorOr.h:156
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20