LLVM 24.0.0git
ObjectFactory.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// These factories reference every object file format. They live apart from
10// the Binary, SymbolicFile and ObjectFile base classes so that a tool which
11// only uses one format does not link all of them.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Object/Archive.h"
23#include "llvm/Object/Wasm.h"
25
26using namespace llvm;
27using namespace llvm::object;
28
30 LLVMContext *Context,
31 bool InitContent) {
33
34 switch (Type) {
36 return Archive::create(Buffer);
37 case file_magic::elf:
63 return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent);
65 return MachOUniversalBinary::create(Buffer);
68 case file_magic::pdb:
69 // PDB does not support the Binary interface.
78 // Unrecognized object file format.
81 auto OffloadBinaryOrErr = OffloadBinary::create(Buffer);
82 if (!OffloadBinaryOrErr)
83 return OffloadBinaryOrErr.takeError();
84 return std::move((*OffloadBinaryOrErr)[0]);
85 }
87 return MinidumpFile::create(Buffer);
89 return TapiUniversal::create(Buffer);
90 }
91 llvm_unreachable("Unexpected Binary File Type");
92}
93
95object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
97 MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
98 /*RequiresNullTerminator=*/false);
99 if (std::error_code EC = FileOrErr.getError())
100 return errorCodeToError(EC);
101 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
102
104 createBinary(Buffer->getMemBufferRef(), Context, InitContent);
105 if (!BinOrErr)
106 return BinOrErr.takeError();
107 std::unique_ptr<Binary> &Bin = BinOrErr.get();
108
109 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
110}
111
114 bool InitContent) {
115 StringRef Data = Object.getBuffer();
118
119 switch (Type) {
127 case file_magic::pdb:
137 case file_magic::elf:
142 return createELFObjectFile(Object, InitContent);
155 return createMachOObjectFile(Object);
159 return createCOFFObjectFile(Object);
165 return createWasmObjectFile(Object);
167 return createDXContainerObjectFile(Object);
169 return createGOFFObjectFile(Object);
170 }
171 llvm_unreachable("Unexpected Object File Type");
172}
173
177 MemoryBuffer::getFile(ObjectPath);
178 if (std::error_code EC = FileOrErr.getError())
179 return errorCodeToError(EC);
180 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
181
183 createObjectFile(Buffer->getMemBufferRef());
184 if (Error Err = ObjOrErr.takeError())
185 return std::move(Err);
186 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
187
188 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
189}
190
193 LLVMContext *Context, bool InitContent) {
194 StringRef Data = Object.getBuffer();
197
198 if (!isSymbolicFile(Type, Context))
200
201 switch (Type) {
203 // Context is guaranteed to be non-null here, because bitcode magic only
204 // indicates a symbolic file when Context is non-null.
205 return IRObjectFile::create(Object, *Context);
206 case file_magic::elf:
227 return ObjectFile::createObjectFile(Object, Type, InitContent);
229 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
234 ObjectFile::createObjectFile(Object, Type, InitContent);
235 if (!Obj || !Context)
236 return std::move(Obj);
237
240 if (!BCData) {
241 consumeError(BCData.takeError());
242 return std::move(Obj);
243 }
244
246 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
247 *Context);
248 }
249 default:
250 llvm_unreachable("Unexpected Binary File Type");
251 }
252}
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
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
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 "-".
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition Archive.cpp:785
MemoryBufferRef Data
Definition Binary.h:38
static Expected< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found.
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
static LLVM_ABI 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< DXContainerObjectFile > > createDXContainerObjectFile(MemoryBufferRef Object)
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Create a MachOObjectFile instance from a given buffer.
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static Expected< std::unique_ptr< ObjectFile > > createGOFFObjectFile(MemoryBufferRef Object)
static Expected< std::unique_ptr< ObjectFile > > createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType)
static LLVM_ABI Expected< SmallVector< std::unique_ptr< OffloadBinary > > > create(MemoryBufferRef Buf, std::optional< uint64_t > Index=std::nullopt)
Attempt to parse the offloading binary stored in Buf.
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source, bool SkipUnknownTriples=false)
static LLVM_ABI Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition Magic.cpp:33
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition Magic.h:21
@ coff_import_library
COFF import library.
Definition Magic.h:49
@ pdb
Windows PDB debug info file.
Definition Magic.h:55
@ spirv_object
A binary SPIR-V file.
Definition Magic.h:62
@ elf_relocatable
ELF Relocatable object file.
Definition Magic.h:28
@ archive
ar style archive file
Definition Magic.h:26
@ elf_shared_object
ELF dynamically linked shared lib.
Definition Magic.h:30
@ goff_object
GOFF object file.
Definition Magic.h:32
@ minidump
Windows minidump file.
Definition Magic.h:46
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition Magic.h:38
@ xcoff_object_64
64-bit XCOFF object file
Definition Magic.h:53
@ elf_executable
ELF Executable image.
Definition Magic.h:29
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition Magic.h:41
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition Magic.h:37
@ offload_bundle
Clang offload bundle file.
Definition Magic.h:60
@ offload_bundle_compressed
Compressed clang offload bundle file.
Definition Magic.h:61
@ macho_file_set
Mach-O file set binary.
Definition Magic.h:45
@ dxcontainer_object
DirectX container file.
Definition Magic.h:59
@ macho_kext_bundle
Mach-O kext bundle file.
Definition Magic.h:43
@ pecoff_executable
PECOFF executable file.
Definition Magic.h:50
@ offload_binary
LLVM offload object file.
Definition Magic.h:58
@ macho_universal_binary
Mach-O universal binary.
Definition Magic.h:44
@ bitcode
Bitcode file.
Definition Magic.h:24
@ macho_core
Mach-O Core File.
Definition Magic.h:36
@ wasm_object
WebAssembly Object file.
Definition Magic.h:54
@ xcoff_object_32
32-bit XCOFF object file
Definition Magic.h:52
@ windows_resource
Windows compiled resource file (.res)
Definition Magic.h:51
@ clang_ast
Clang PCH or PCM.
Definition Magic.h:25
@ elf_core
ELF core image.
Definition Magic.h:31
@ macho_object
Mach-O Object file.
Definition Magic.h:33
@ coff_object
COFF object file.
Definition Magic.h:48
@ elf
ELF Unknown type.
Definition Magic.h:27
@ macho_bundle
Mach-O Bundle file.
Definition Magic.h:40
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition Magic.h:47
@ cuda_fatbinary
CUDA Fatbinary object file.
Definition Magic.h:57
@ macho_executable
Mach-O Executable.
Definition Magic.h:34
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition Magic.h:42
@ unknown
Unrecognized file.
Definition Magic.h:23
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition Magic.h:35
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition Magic.h:39
@ tapi_file
Text-based Dynamic Library Stub file.
Definition Magic.h:56