LLVM  5.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"
19 #include "llvm/IR/GVMaterializer.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/Object/ObjectFile.h"
33 #include "llvm/Support/SourceMgr.h"
36 using namespace llvm;
37 using namespace object;
38 
39 IRObjectFile::IRObjectFile(MemoryBufferRef Object,
40  std::vector<std::unique_ptr<Module>> Mods)
41  : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
42  for (auto &M : this->Mods)
43  SymTab.addModule(M.get());
44 }
45 
46 IRObjectFile::~IRObjectFile() {}
47 
48 static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
49  return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
50 }
51 
52 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
53  Symb.p += sizeof(ModuleSymbolTable::Symbol);
54 }
55 
56 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
57  DataRefImpl Symb) const {
58  SymTab.printSymbolName(OS, getSym(Symb));
59  return std::error_code();
60 }
61 
62 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
63  return SymTab.getSymbolFlags(getSym(Symb));
64 }
65 
66 basic_symbol_iterator IRObjectFile::symbol_begin() const {
68  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
69  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
70 }
71 
72 basic_symbol_iterator IRObjectFile::symbol_end() const {
74  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
75  SymTab.symbols().size());
76  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
77 }
78 
79 StringRef IRObjectFile::getTargetTriple() const {
80  // Each module must have the same target triple, so we arbitrarily access the
81  // first one.
82  return Mods[0]->getTargetTriple();
83 }
84 
85 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
86  for (const SectionRef &Sec : Obj.sections()) {
87  if (Sec.isBitcode()) {
88  StringRef SecContents;
89  if (std::error_code EC = Sec.getContents(SecContents))
90  return EC;
91  return MemoryBufferRef(SecContents, Obj.getFileName());
92  }
93  }
94 
95  return object_error::bitcode_section_not_found;
96 }
97 
98 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
100  switch (Type) {
101  case file_magic::bitcode:
102  return Object;
103  case file_magic::elf_relocatable:
104  case file_magic::macho_object:
105  case file_magic::coff_object: {
107  ObjectFile::createObjectFile(Object, Type);
108  if (!ObjFile)
109  return errorToErrorCode(ObjFile.takeError());
110  return findBitcodeInObject(*ObjFile->get());
111  }
112  default:
113  return object_error::invalid_file_type;
114  }
115 }
116 
118 IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
119  ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
120  if (!BCOrErr)
121  return errorCodeToError(BCOrErr.getError());
122 
124  getBitcodeModuleList(*BCOrErr);
125  if (!BMsOrErr)
126  return BMsOrErr.takeError();
127 
128  std::vector<std::unique_ptr<Module>> Mods;
129  for (auto BM : *BMsOrErr) {
131  BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
132  /*IsImporting*/ false);
133  if (!MOrErr)
134  return MOrErr.takeError();
135 
136  Mods.push_back(std::move(*MOrErr));
137  }
138 
139  return std::unique_ptr<IRObjectFile>(
140  new IRObjectFile(*BCOrErr, std::move(Mods)));
141 }
142 
144  IRSymtabFile F;
145  ErrorOr<MemoryBufferRef> BCOrErr =
146  IRObjectFile::findBitcodeInMemBuffer(MBRef);
147  if (!BCOrErr)
148  return errorCodeToError(BCOrErr.getError());
149 
151  if (!BFCOrErr)
152  return BFCOrErr.takeError();
153 
155  if (!FCOrErr)
156  return FCOrErr.takeError();
157 
158  F.Mods = std::move(BFCOrErr->Mods);
159  F.Symtab = std::move(FCOrErr->Symtab);
160  F.Strtab = std::move(FCOrErr->Strtab);
161  F.TheReader = std::move(FCOrErr->TheReader);
162  return std::move(F);
163 }
std::error_code getError() const
Definition: ErrorOr.h:172
Represents either an error or a value T.
Definition: ErrorOr.h:69
LLVMContext & Context
This class is the base class for all object file types.
Definition: ObjectFile.h:189
irsymtab::Reader TheReader
Definition: IRObjectFile.h:72
Error takeError()
Take ownership of the stored error.
Definition: Error.h:764
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
write bitcode
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
#define F(x, y, z)
Definition: MD5.cpp:55
The contents of a bitcode file and its irsymtab.
Definition: IRObjectFile.h:69
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:78
std::vector< BitcodeModule > Mods
Definition: IRObjectFile.h:70
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
section_iterator_range sections() const
Definition: ObjectFile.h:272
StringRef getFileName() const
Definition: Binary.cpp:41
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
Module.h This file contains the declarations for the Module class.
SmallVector< char, 0 > Strtab
Definition: IRObjectFile.h:71
reference get()
Returns a reference to the stored T value.
Definition: Error.h:744
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:316
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:139
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:99
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:49
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:84
SmallVector< char, 0 > Symtab
Definition: IRObjectFile.h:71
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:21