LLVM  12.0.0git
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 //===----------------------------------------------------------------------===//
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>
29 using namespace llvm;
30 using namespace object;
33  : Binary(Type, Source) {}
35 SymbolicFile::~SymbolicFile() = default;
40  StringRef Data = Object.getBuffer();
41  if (Type == file_magic::unknown)
42  Type = identify_magic(Data);
44  switch (Type) {
46  if (Context)
47  return IRObjectFile::create(Object, *Context);
54  case file_magic::pdb:
58  case file_magic::elf:
76  return ObjectFile::createObjectFile(Object, Type);
78  return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
83  ObjectFile::createObjectFile(Object, Type);
84  if (!Obj || !Context)
85  return std::move(Obj);
89  if (!BCData) {
90  consumeError(BCData.takeError());
91  return std::move(Obj);
92  }
94  return IRObjectFile::create(
95  MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
96  *Context);
97  }
98  }
99  llvm_unreachable("Unexpected Binary File Type");
100 }
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:185
32-bit XCOFF object file
Definition: Magic.h:48
Mach-O Object file.
Definition: Magic.h:30
LLVMContext & Context
ELF core image.
Definition: Magic.h:29
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:28
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context)
Windows compiled resource file (.res)
Definition: Magic.h:47
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
Mach-O Bundle file.
Definition: Magic.h:37
Mach-O Preloaded Executable.
Definition: Magic.h:34
Bitcode file.
Definition: Magic.h:23
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
StringRef getBuffer() const
Definition: MemoryBuffer.h:274
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:35
The Mach-O dynamic linker.
Definition: Magic.h:36
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
64-bit XCOFF object file
Definition: Magic.h:49
ELF Relocatable object file.
Definition: Magic.h:26
Windows minidump file.
Definition: Magic.h:42
COFF import library.
Definition: Magic.h:45
Mach-O universal binary.
Definition: Magic.h:41
Mach-O Core File.
Definition: Magic.h:33
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
Mach-O dSYM companion file.
Definition: Magic.h:39
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
PECOFF executable file.
Definition: Magic.h:46
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
COFF object file.
Definition: Magic.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Mach-O kext bundle file.
Definition: Magic.h:40
ar style archive file
Definition: Magic.h:24
static Expected< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found...
reference get()
Returns a reference to the stored T value.
Definition: Error.h:537
ELF Unknown type.
Definition: Magic.h:25
ELF Executable image.
Definition: Magic.h:27
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:276
WebAssembly Object file.
Definition: Magic.h:50
Provides ErrorOr<T> smart pointer.
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:280
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Mach-O Executable.
Definition: Magic.h:31
Unrecognized file.
Definition: Magic.h:22
Windows PDB debug info file.
Definition: Magic.h:51
Text-based Dynamic Library Stub file.
Definition: Magic.h:52
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20