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/ObjectFile.h"
21 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorOr.h"
26 #include <algorithm>
27 #include <memory>
28 #include <system_error>
30 using namespace llvm;
31 using namespace object;
33 Binary::~Binary() = default;
36  : TypeID(Type), Data(Source) {}
38 StringRef Binary::getData() const { return Data.getBuffer(); }
48  switch (Type) {
50  return Archive::create(Buffer);
51  case file_magic::elf:
72  return ObjectFile::createSymbolicFile(Buffer, Type, Context);
74  return MachOUniversalBinary::create(Buffer);
77  case file_magic::pdb:
78  // PDB does not support the Binary interface.
82  // Unrecognized object file format.
84  }
85  llvm_unreachable("Unexpected Binary File Type");
86 }
90  MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
91  /*RequiresNullTerminator=*/false);
92  if (std::error_code EC = FileOrErr.getError())
93  return errorCodeToError(EC);
94  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
97  createBinary(Buffer->getMemBufferRef());
98  if (!BinOrErr)
99  return BinOrErr.takeError();
100  std::unique_ptr<Binary> &Bin = BinOrErr.get();
102  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
103 }
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:42
ELF dynamically linked shared lib.
Definition: Magic.h:28
StringRef getFileName() const
Definition: Binary.cpp:40
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context)
Windows compiled resource file (.res)
Definition: Magic.h:46
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:44
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:42
COFF import library.
Definition: Magic.h:44
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
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:45
COFF object file.
Definition: Magic.h:43
#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:36
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:274
WebAssembly Object file.
Definition: Magic.h:47
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:38
Windows PDB debug info file.
Definition: Magic.h:48
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