LLVM  15.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"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/Error.h"
22 #include <memory>
23 
24 using namespace llvm;
25 using namespace object;
26 
27 namespace llvm {
28 class LLVMContext;
29 }
30 
32  : Binary(Type, Source) {}
33 
34 SymbolicFile::~SymbolicFile() = default;
35 
38  LLVMContext *Context, bool InitContent) {
39  StringRef Data = Object.getBuffer();
42 
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:
70  return ObjectFile::createObjectFile(Object, Type, InitContent);
72  return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
77  ObjectFile::createObjectFile(Object, Type, InitContent);
78  if (!Obj || !Context)
79  return std::move(Obj);
80 
83  if (!BCData) {
84  consumeError(BCData.takeError());
85  return std::move(Obj);
86  }
87 
88  return IRObjectFile::create(
89  MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
90  *Context);
91  }
92  default:
93  llvm_unreachable("Unexpected Binary File Type");
94  }
95 }
96 
98  switch (Type) {
100  return Context != nullptr;
101  case file_magic::elf:
124  return true;
125  default:
126  return false;
127  }
128 }
llvm::file_magic::unknown
@ unknown
Unrecognized file.
Definition: Magic.h:22
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::file_magic
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
StringRef.h
llvm::file_magic::elf_core
@ elf_core
ELF core image.
Definition: Magic.h:29
llvm::file_magic::macho_dynamic_linker
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:37
ErrorHandling.h
llvm::file_magic::elf_shared_object
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:28
Error.h
llvm::file_magic::goff_object
@ goff_object
GOFF object file.
Definition: Magic.h:30
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
COFFImportFile.h
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
SymbolicFile.h
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::file_magic::bitcode
@ bitcode
Bitcode file.
Definition: Magic.h:23
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
Error.h
Magic.h
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:90
llvm::file_magic::xcoff_object_64
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:50
llvm::file_magic::macho_preload_executable
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:35
llvm::file_magic::macho_bundle
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:38
llvm::file_magic::macho_core
@ macho_core
Mach-O Core File.
Definition: Magic.h:34
llvm::file_magic::macho_dsym_companion
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:40
llvm::object::object_error::invalid_file_type
@ invalid_file_type
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
llvm::file_magic::coff_import_library
@ coff_import_library
COFF import library.
Definition: Magic.h:46
llvm::OutputFileType::Object
@ Object
llvm::object::SymbolicFile::isSymbolicFile
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
Definition: SymbolicFile.cpp:97
llvm::file_magic::pecoff_executable
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:47
llvm::object::IRObjectFile::findBitcodeInObject
static Expected< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found.
Definition: IRObjectFile.cpp:74
IRObjectFile.h
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::file_magic::macho_dynamically_linked_shared_lib
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:36
llvm::file_magic::macho_dynamically_linked_shared_lib_stub
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:39
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition: Magic.h:45
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:188
ObjectFile.h
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
llvm::file_magic::macho_kext_bundle
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:41
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
llvm::object::SymbolicFile::SymbolicFile
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
Definition: SymbolicFile.cpp:31
llvm::file_magic::macho_fixed_virtual_memory_shared_lib
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:33
llvm::file_magic::elf
@ elf
ELF Unknown type.
Definition: Magic.h:25
llvm::object::SymbolicFile::createSymbolicFile
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
llvm::file_magic::elf_executable
@ elf_executable
ELF Executable image.
Definition: Magic.h:27
llvm::object::COFFImportFile
Definition: COFFImportFile.h:29
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::object::IRObjectFile::create
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
Definition: IRObjectFile.cpp:111
llvm::file_magic::wasm_object
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:51
llvm::file_magic::macho_executable
@ macho_executable
Mach-O Executable.
Definition: Magic.h:32
llvm::object::SymbolicFile::~SymbolicFile
~SymbolicFile() override
llvm::identify_magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
llvm::file_magic::xcoff_object_32
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:49