LLVM  10.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"
23 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorOr.h"
28 #include <algorithm>
29 #include <memory>
30 #include <system_error>
31 
32 using namespace llvm;
33 using namespace object;
34 
35 Binary::~Binary() = default;
36 
38  : TypeID(Type), Data(Source) {}
39 
40 StringRef Binary::getData() const { return Data.getBuffer(); }
41 
43 
45 
49 
50  switch (Type) {
52  return Archive::create(Buffer);
53  case file_magic::elf:
76  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
78  return MachOUniversalBinary::create(Buffer);
81  case file_magic::pdb:
82  // PDB does not support the Binary interface.
86  // Unrecognized object file format.
89  return MinidumpFile::create(Buffer);
91  return TapiUniversal::create(Buffer);
92  }
93  llvm_unreachable("Unexpected Binary File Type");
94 }
95 
98  MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
99  /*RequiresNullTerminator=*/false);
100  if (std::error_code EC = FileOrErr.getError())
101  return errorCodeToError(EC);
102  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
103 
105  createBinary(Buffer->getMemBufferRef());
106  if (!BinOrErr)
107  return BinOrErr.takeError();
108  std::unique_ptr<Binary> &Bin = BinOrErr.get();
109 
110  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
111 }
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:42
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:46
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: yaml2obj.h:21
64-bit XCOFF object file
Definition: Magic.h:49
ELF Relocatable object file.
Definition: Magic.h:26
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:44
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
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source)
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:40
Windows PDB debug info file.
Definition: Magic.h:51
Text-based Dynamic Library Stub file.
Definition: Magic.h:52
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