LLVM 17.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"
24#include "llvm/Support/Error.h"
28#include <memory>
29#include <system_error>
30
31using namespace llvm;
32using namespace object;
33
34Binary::~Binary() = default;
35
36Binary::Binary(unsigned int Type, MemoryBufferRef Source)
37 : TypeID(Type), Data(Source) {}
38
40
42
44
46 LLVMContext *Context,
47 bool InitContent) {
49
50 switch (Type) {
52 return Archive::create(Buffer);
53 case file_magic::elf:
78 return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent);
80 return MachOUniversalBinary::create(Buffer);
83 case file_magic::pdb:
84 // PDB does not support the Binary interface.
90 // Unrecognized object file format.
93 return OffloadBinary::create(Buffer);
95 return MinidumpFile::create(Buffer);
97 return TapiUniversal::create(Buffer);
98 }
99 llvm_unreachable("Unexpected Binary File Type");
100}
101
103object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
105 MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
106 /*RequiresNullTerminator=*/false);
107 if (std::error_code EC = FileOrErr.getError())
108 return errorCodeToError(EC);
109 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
110
112 createBinary(Buffer->getMemBufferRef(), Context, InitContent);
113 if (!BinOrErr)
114 return BinOrErr.takeError();
115 std::unique_ptr<Binary> &Bin = BinOrErr.get();
116
117 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
118}
Provides ErrorOr<T> smart pointer.
LLVMContext & Context
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:150
std::error_code getError() const
Definition: ErrorOr.h:153
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringRef getBufferIdentifier() const
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:660
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getData() const
Definition: Binary.cpp:39
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
StringRef getFileName() const
Definition: Binary.cpp:41
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
static Expected< std::unique_ptr< MinidumpFile > > create(MemoryBufferRef Source)
Construct a new MinidumpFile object from the given memory buffer.
Definition: Minidump.cpp:112
static Expected< std::unique_ptr< OffloadBinary > > create(MemoryBufferRef)
Attempt to parse the offloading binary stored in Data.
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source)
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
@ coff_import_library
COFF import library.
Definition: Magic.h:47
@ pdb
Windows PDB debug info file.
Definition: Magic.h:53
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
@ archive
ar style archive file
Definition: Magic.h:24
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:28
@ goff_object
GOFF object file.
Definition: Magic.h:30
@ minidump
Windows minidump file.
Definition: Magic.h:44
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:36
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:51
@ elf_executable
ELF Executable image.
Definition: Magic.h:27
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:39
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:35
@ macho_file_set
Mach-O file set binary.
Definition: Magic.h:43
@ dxcontainer_object
DirectX container file.
Definition: Magic.h:57
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:41
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:48
@ offload_binary
LLVM offload object file.
Definition: Magic.h:56
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:42
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ macho_core
Mach-O Core File.
Definition: Magic.h:34
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:52
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:50
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:49
@ elf_core
ELF core image.
Definition: Magic.h:29
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
@ coff_object
COFF object file.
Definition: Magic.h:46
@ elf
ELF Unknown type.
Definition: Magic.h:25
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:38
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:45
@ cuda_fatbinary
CUDA Fatbinary object file.
Definition: Magic.h:55
@ macho_executable
Mach-O Executable.
Definition: Magic.h:32
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:40
@ unknown
Unrecognized file.
Definition: Magic.h:22
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:33
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:37
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:54