LLVM 22.0.0git
SymbolicFile.cpp
Go to the documentation of this file.
1//===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
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 a file format independent SymbolicFile class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/StringRef.h"
17#include "llvm/Object/Error.h"
20#include "llvm/Support/Error.h"
22#include <memory>
23
24using namespace llvm;
25using namespace object;
26
27namespace llvm {
28class LLVMContext;
29}
30
32 : Binary(Type, Source) {}
33
35
38 LLVMContext *Context, bool InitContent) {
39 StringRef Data = Object.getBuffer();
42
43 if (!isSymbolicFile(Type, Context))
45
46 switch (Type) {
48 // Context is guaranteed to be non-null here, because bitcode magic only
49 // indicates a symbolic file when Context is non-null.
50 return IRObjectFile::create(Object, *Context);
51 case file_magic::elf:
72 return ObjectFile::createObjectFile(Object, Type, InitContent);
74 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
79 ObjectFile::createObjectFile(Object, Type, InitContent);
80 if (!Obj || !Context)
81 return std::move(Obj);
82
85 if (!BCData) {
86 consumeError(BCData.takeError());
87 return std::move(Obj);
88 }
89
91 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
92 *Context);
93 }
94 default:
95 llvm_unreachable("Unexpected Binary File Type");
96 }
97}
98
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 - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
MemoryBufferRef Data
Definition Binary.h:38
Binary(unsigned int Type, MemoryBufferRef Source)
Definition Binary.cpp:36
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< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:111
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
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
@ elf_relocatable
ELF Relocatable object file.
Definition Magic.h:28
@ elf_shared_object
ELF dynamically linked shared lib.
Definition Magic.h:30
@ goff_object
GOFF object file.
Definition Magic.h:32
@ 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
@ 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
@ 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
@ 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
@ 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