LLVM  14.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/Compiler.h"
21 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorOr.h"
26 #include <algorithm>
27 #include <memory>
28 
29 using namespace llvm;
30 using namespace object;
31 
33  : Binary(Type, Source) {}
34 
35 SymbolicFile::~SymbolicFile() = default;
36 
39  LLVMContext *Context, bool InitContent) {
40  StringRef Data = Object.getBuffer();
43 
46 
47  switch (Type) {
49  // Context is guaranteed to be non-null here, because bitcode magic only
50  // indicates a symbolic file when Context is non-null.
51  return IRObjectFile::create(Object, *Context);
52  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 
89  return IRObjectFile::create(
90  MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
91  *Context);
92  }
93  default:
94  llvm_unreachable("Unexpected Binary File Type");
95  }
96 }
97 
99  switch (Type) {
100  case file_magic::bitcode:
101  return Context != nullptr;
102  case file_magic::elf:
125  return true;
126  default:
127  return false;
128  }
129 }
MemoryBuffer.h
llvm::file_magic::unknown
@ unknown
Unrecognized file.
Definition: Magic.h:22
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
FileSystem.h
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:42
SymbolicFile.h
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
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:87
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:98
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
ErrorOr.h
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:185
ObjectFile.h
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
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:134
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:569
Compiler.h
llvm::object::SymbolicFile::SymbolicFile
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
Definition: SymbolicFile.cpp:32
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:30
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
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:35
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