LLVM 20.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:
71 return ObjectFile::createObjectFile(Object, Type, InitContent);
73 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
78 ObjectFile::createObjectFile(Object, Type, InitContent);
79 if (!Obj || !Context)
80 return std::move(Obj);
81
84 if (!BCData) {
85 consumeError(BCData.takeError());
86 return std::move(Obj);
87 }
88
90 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
91 *Context);
92 }
93 default:
94 llvm_unreachable("Unexpected Binary File Type");
95 }
96}
97
99 switch (Type) {
101 return Context != nullptr;
102 case file_magic::elf:
126 return true;
127 default:
128 return false;
129 }
130}
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 - 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
MemoryBufferRef Data
Definition: Binary.h:37
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)
Definition: ObjectFile.cpp:209
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.
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
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
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
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:27
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:29
@ goff_object
GOFF object file.
Definition: Magic.h:31
@ 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
@ macho_file_set
Mach-O file set binary.
Definition: Magic.h:44
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:42
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:49
@ 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
@ 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
@ 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