LLVM  13.0.0git
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 
48  bool InitContent) {
50 
51  switch (Type) {
53  return Archive::create(Buffer);
54  case file_magic::elf:
77  return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent);
79  return MachOUniversalBinary::create(Buffer);
82  case file_magic::pdb:
83  // PDB does not support the Binary interface.
87  // Unrecognized object file format.
90  return MinidumpFile::create(Buffer);
92  return TapiUniversal::create(Buffer);
93  }
94  llvm_unreachable("Unexpected Binary File Type");
95 }
96 
100  MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
101  /*RequiresNullTerminator=*/false);
102  if (std::error_code EC = FileOrErr.getError())
103  return errorCodeToError(EC);
104  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
105 
107  createBinary(Buffer->getMemBufferRef(), Context, InitContent);
108  if (!BinOrErr)
109  return BinOrErr.takeError();
110  std::unique_ptr<Binary> &Bin = BinOrErr.get();
111 
112  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
113 }
MemoryBuffer.h
llvm::file_magic::unknown
@ unknown
Unrecognized file.
Definition: Magic.h:22
llvm
Definition: AllocatorList.h:23
llvm::object::WindowsResource::createWindowsResource
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
Definition: WindowsResource.cpp:60
FileSystem.h
llvm::file_magic
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
StringRef.h
llvm::file_magic::elf_core
@ elf_core
ELF core image.
Definition: Magic.h:29
llvm::file_magic::pdb
@ pdb
Windows PDB debug info file.
Definition: Magic.h:51
llvm::file_magic::macho_dynamic_linker
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:36
ErrorHandling.h
llvm::file_magic::elf_shared_object
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:28
Error.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
TapiUniversal.h
llvm::file_magic::minidump
@ minidump
Windows minidump file.
Definition: Magic.h:42
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
WindowsResource.h
llvm::file_magic::bitcode
@ bitcode
Bitcode file.
Definition: Magic.h:23
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
Error.h
Magic.h
llvm::object::Binary::getMemoryBufferRef
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:44
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
llvm::file_magic::xcoff_object_64
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:49
MachOUniversal.h
llvm::file_magic::macho_preload_executable
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:34
llvm::file_magic::macho_bundle
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:37
llvm::file_magic::macho_core
@ macho_core
Mach-O Core File.
Definition: Magic.h:33
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
llvm::file_magic::macho_dsym_companion
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:39
llvm::object::object_error::invalid_file_type
@ invalid_file_type
llvm::object::Binary::~Binary
virtual ~Binary()
llvm::object::MinidumpFile::create
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:111
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
llvm::file_magic::coff_import_library
@ coff_import_library
COFF import library.
Definition: Magic.h:45
llvm::file_magic::pecoff_executable
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:46
llvm::file_magic::macho_universal_binary
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:41
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::file_magic::macho_dynamically_linked_shared_lib
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:35
llvm::file_magic::macho_dynamically_linked_shared_lib_stub
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:38
llvm::object::TapiUniversal::create
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source)
Definition: TapiUniversal.cpp:55
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
ErrorOr.h
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition: Magic.h:44
Archive.h
ObjectFile.h
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::file_magic::macho_kext_bundle
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:40
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:537
llvm::file_magic::macho_fixed_virtual_memory_shared_lib
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:32
llvm::object::MachOUniversalBinary::create
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Definition: MachOUniversal.cpp:123
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::file_magic::elf
@ elf
ELF Unknown type.
Definition: Magic.h:25
llvm::object::SymbolicFile::createSymbolicFile
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
llvm::file_magic::archive
@ archive
ar style archive file
Definition: Magic.h:24
llvm::file_magic::elf_executable
@ elf_executable
ELF Executable image.
Definition: Magic.h:27
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
Binary.h
Minidump.h
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:42
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::file_magic::wasm_object
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:50
llvm::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
llvm::object::createBinary
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:46
llvm::file_magic::coff_cl_gl_object
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:43
llvm::object::Binary::getData
StringRef getData() const
Definition: Binary.cpp:40
llvm::file_magic::macho_executable
@ macho_executable
Mach-O Executable.
Definition: Magic.h:31
llvm::Type::TypeID
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:55
llvm::file_magic::tapi_file
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:52
llvm::identify_magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:35
llvm::object::Archive::create
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:526
llvm::file_magic::windows_resource
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:47
llvm::object::Binary::Binary
Binary()=delete
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition: Magic.h:30
llvm::file_magic::xcoff_object_32
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:48