LLVM  9.0.0svn
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 //===----------------------------------------------------------------------===//
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>
31 using namespace llvm;
32 using namespace object;
34 Binary::~Binary() = default;
37  : TypeID(Type), Data(Source) {}
39 StringRef Binary::getData() const { return Data.getBuffer(); }
49  switch (Type) {
51  return Archive::create(Buffer);
52  case file_magic::elf:
74  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
76  return MachOUniversalBinary::create(Buffer);
79  case file_magic::pdb:
80  // PDB does not support the Binary interface.
84  // Unrecognized object file format.
87  return MinidumpFile::create(Buffer);
88  }
89  llvm_unreachable("Unexpected Binary File Type");
90 }
94  MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
95  /*RequiresNullTerminator=*/false);
96  if (std::error_code EC = FileOrErr.getError())
97  return errorCodeToError(EC);
98  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
101  createBinary(Buffer->getMemBufferRef());
102  if (!BinOrErr)
103  return BinOrErr.takeError();
104  std::unique_ptr<Binary> &Bin = BinOrErr.get();
106  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
107 }
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
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
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:49
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:50
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