LLVM 20.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.
94 // Unrecognized object file format.
97 return OffloadBinary::create(Buffer);
99 return MinidumpFile::create(Buffer);
101 return TapiUniversal::create(Buffer);
102 }
103 llvm_unreachable("Unexpected Binary File Type");
104}
105
107object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
109 MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
110 /*RequiresNullTerminator=*/false);
111 if (std::error_code EC = FileOrErr.getError())
112 return errorCodeToError(EC);
113 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
114
116 createBinary(Buffer->getMemBufferRef(), Context, InitContent);
117 if (!BinOrErr)
118 return BinOrErr.takeError();
119 std::unique_ptr<Binary> &Bin = BinOrErr.get();
120
121 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
122}
Provides ErrorOr<T> smart pointer.
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
std::error_code getError() const
Definition: ErrorOr.h:152
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
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:51
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:668
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:91
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:111
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:48
@ pdb
Windows PDB debug info file.
Definition: Magic.h:54
@ spirv_object
A binary SPIR-V file.
Definition: Magic.h:61
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:27
@ archive
ar style archive file
Definition: Magic.h:25
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:29
@ goff_object
GOFF object file.
Definition: Magic.h:31
@ minidump
Windows minidump file.
Definition: Magic.h:45
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:37
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:52
@ elf_executable
ELF Executable image.
Definition: Magic.h:28
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:40
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:36
@ offload_bundle
Clang offload bundle file.
Definition: Magic.h:59
@ offload_bundle_compressed
Compressed clang offload bundle file.
Definition: Magic.h:60
@ macho_file_set
Mach-O file set binary.
Definition: Magic.h:44
@ dxcontainer_object
DirectX container file.
Definition: Magic.h:58
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:42
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:49
@ offload_binary
LLVM offload object file.
Definition: Magic.h:57
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:43
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ macho_core
Mach-O Core File.
Definition: Magic.h:35
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:53
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:51
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:50
@ clang_ast
Clang PCH or PCM.
Definition: Magic.h:24
@ elf_core
ELF core image.
Definition: Magic.h:30
@ macho_object
Mach-O Object file.
Definition: Magic.h:32
@ coff_object
COFF object file.
Definition: Magic.h:47
@ elf
ELF Unknown type.
Definition: Magic.h:26
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:39
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:46
@ cuda_fatbinary
CUDA Fatbinary object file.
Definition: Magic.h:56
@ macho_executable
Mach-O Executable.
Definition: Magic.h:33
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:41
@ unknown
Unrecognized file.
Definition: Magic.h:22
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:34
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:38
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:55