LLVM  4.0.0
IRObjectFile.cpp
Go to the documentation of this file.
1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the IRObjectFile class implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "RecordStreamer.h"
16 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/IR/GVMaterializer.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Support/SourceMgr.h"
35 using namespace llvm;
36 using namespace object;
37 
38 IRObjectFile::IRObjectFile(MemoryBufferRef Object,
39  std::vector<std::unique_ptr<Module>> Mods)
40  : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
41  for (auto &M : this->Mods)
42  SymTab.addModule(M.get());
43 }
44 
46 
48  return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
49 }
50 
52  Symb.p += sizeof(ModuleSymbolTable::Symbol);
53 }
54 
56  DataRefImpl Symb) const {
57  SymTab.printSymbolName(OS, getSym(Symb));
58  return std::error_code();
59 }
60 
62  return SymTab.getSymbolFlags(getSym(Symb));
63 }
64 
67  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
68  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
69 }
70 
73  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
74  SymTab.symbols().size());
75  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
76 }
77 
79  // Each module must have the same target triple, so we arbitrarily access the
80  // first one.
81  return Mods[0]->getTargetTriple();
82 }
83 
85  for (const SectionRef &Sec : Obj.sections()) {
86  if (Sec.isBitcode()) {
87  StringRef SecContents;
88  if (std::error_code EC = Sec.getContents(SecContents))
89  return EC;
90  return MemoryBufferRef(SecContents, Obj.getFileName());
91  }
92  }
93 
95 }
96 
99  switch (Type) {
101  return Object;
106  ObjectFile::createObjectFile(Object, Type);
107  if (!ObjFile)
108  return errorToErrorCode(ObjFile.takeError());
109  return findBitcodeInObject(*ObjFile->get());
110  }
111  default:
113  }
114 }
115 
119  if (!BCOrErr)
120  return errorCodeToError(BCOrErr.getError());
121 
123  getBitcodeModuleList(*BCOrErr);
124  if (!BMsOrErr)
125  return BMsOrErr.takeError();
126 
127  std::vector<std::unique_ptr<Module>> Mods;
128  for (auto BM : *BMsOrErr) {
130  BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
131  /*IsImporting*/ false);
132  if (!MOrErr)
133  return MOrErr.takeError();
134 
135  Mods.push_back(std::move(*MOrErr));
136  }
137 
138  return std::unique_ptr<IRObjectFile>(
139  new IRObjectFile(*BCOrErr, std::move(Mods)));
140 }
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:116
std::error_code getError() const
Definition: ErrorOr.h:169
Represents either an error or a value T.
Definition: ErrorOr.h:68
LLVMContext & Context
This class is the base class for all object file types.
Definition: ObjectFile.h:178
ELF Relocatable object file.
Definition: FileSystem.h:246
Error takeError()
Take ownership of the stored error.
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
Tagged union holding either a T or a Error.
basic_symbol_iterator symbol_begin() const override
static ErrorOr< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
basic_symbol_iterator symbol_end() const override
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:48
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
StringRef getTargetTriple() const
section_iterator_range sections() const
Definition: ObjectFile.h:257
StringRef getFileName() const
Definition: Binary.cpp:35
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:999
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:126
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
uint32_t getSymbolFlags(Symbol S) const
void moveSymbolNext(DataRefImpl &Symb) const override
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:240
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:86
ArrayRef< Symbol > symbols() const
void printSymbolName(raw_ostream &OS, Symbol S) const
static ErrorOr< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:70
uint32_t getSymbolFlags(DataRefImpl Symb) const override
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87