LLVM  9.0.0svn
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 
40  StringRef Data = Object.getBuffer();
41  if (Type == file_magic::unknown)
42  Type = identify_magic(Data);
43 
44  switch (Type) {
46  if (Context)
47  return IRObjectFile::create(Object, *Context);
54  case file_magic::pdb:
57  case file_magic::elf:
74  return ObjectFile::createObjectFile(Object, Type);
76  return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
81  ObjectFile::createObjectFile(Object, Type);
82  if (!Obj || !Context)
83  return std::move(Obj);
84 
87  if (!BCData) {
88  consumeError(BCData.takeError());
89  return std::move(Obj);
90  }
91 
92  return IRObjectFile::create(
93  MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
94  *Context);
95  }
96  }
97  llvm_unreachable("Unexpected Binary File Type");
98 }
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:163
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:552
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:272
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
The Mach-O dynamic linker.
Definition: Magic.h:36
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
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:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
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:981
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:532
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:274
WebAssembly Object file.
Definition: Magic.h:49
Provides ErrorOr<T> smart pointer.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Mach-O Executable.
Definition: Magic.h:31
Unrecognized file.
Definition: Magic.h:22
Windows PDB debug info file.
Definition: Magic.h:50
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20