LLVM  8.0.1
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 "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/Object/ObjectFile.h"
26 using namespace llvm;
27 using namespace object;
28 
29 IRObjectFile::IRObjectFile(MemoryBufferRef Object,
30  std::vector<std::unique_ptr<Module>> Mods)
31  : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
32  for (auto &M : this->Mods)
33  SymTab.addModule(M.get());
34 }
35 
36 IRObjectFile::~IRObjectFile() {}
37 
38 static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
39  return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
40 }
41 
42 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
43  Symb.p += sizeof(ModuleSymbolTable::Symbol);
44 }
45 
46 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
47  DataRefImpl Symb) const {
48  SymTab.printSymbolName(OS, getSym(Symb));
49  return std::error_code();
50 }
51 
52 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
53  return SymTab.getSymbolFlags(getSym(Symb));
54 }
55 
56 basic_symbol_iterator IRObjectFile::symbol_begin() const {
58  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
59  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
60 }
61 
62 basic_symbol_iterator IRObjectFile::symbol_end() const {
64  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
65  SymTab.symbols().size());
66  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
67 }
68 
69 StringRef IRObjectFile::getTargetTriple() const {
70  // Each module must have the same target triple, so we arbitrarily access the
71  // first one.
72  return Mods[0]->getTargetTriple();
73 }
74 
76 IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
77  for (const SectionRef &Sec : Obj.sections()) {
78  if (Sec.isBitcode()) {
79  StringRef SecContents;
80  if (std::error_code EC = Sec.getContents(SecContents))
81  return errorCodeToError(EC);
82  return MemoryBufferRef(SecContents, Obj.getFileName());
83  }
84  }
85 
86  return errorCodeToError(object_error::bitcode_section_not_found);
87 }
88 
90 IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
92  switch (Type) {
94  return Object;
95  case file_magic::elf_relocatable:
96  case file_magic::macho_object:
97  case file_magic::coff_object: {
99  ObjectFile::createObjectFile(Object, Type);
100  if (!ObjFile)
101  return ObjFile.takeError();
102  return findBitcodeInObject(*ObjFile->get());
103  }
104  default:
105  return errorCodeToError(object_error::invalid_file_type);
106  }
107 }
108 
110 IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
111  Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
112  if (!BCOrErr)
113  return BCOrErr.takeError();
114 
116  getBitcodeModuleList(*BCOrErr);
117  if (!BMsOrErr)
118  return BMsOrErr.takeError();
119 
120  std::vector<std::unique_ptr<Module>> Mods;
121  for (auto BM : *BMsOrErr) {
123  BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
124  /*IsImporting*/ false);
125  if (!MOrErr)
126  return MOrErr.takeError();
127 
128  Mods.push_back(std::move(*MOrErr));
129  }
130 
131  return std::unique_ptr<IRObjectFile>(
132  new IRObjectFile(*BCOrErr, std::move(Mods)));
133 }
134 
136  IRSymtabFile F;
137  Expected<MemoryBufferRef> BCOrErr =
138  IRObjectFile::findBitcodeInMemBuffer(MBRef);
139  if (!BCOrErr)
140  return BCOrErr.takeError();
141 
143  if (!BFCOrErr)
144  return BFCOrErr.takeError();
145 
147  if (!FCOrErr)
148  return FCOrErr.takeError();
149 
150  F.Mods = std::move(BFCOrErr->Mods);
151  F.Symtab = std::move(FCOrErr->Symtab);
152  F.Strtab = std::move(FCOrErr->Strtab);
153  F.TheReader = std::move(FCOrErr->TheReader);
154  return std::move(F);
155 }
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringRef getFileName() const
Definition: Binary.cpp:41
This class is the base class for all object file types.
Definition: ObjectFile.h:202
F(f)
irsymtab::Reader TheReader
Definition: IRObjectFile.h:83
Error takeError()
Take ownership of the stored error.
Definition: Error.h:553
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb)
Definition: BitVector.h:938
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
StringRef getBuffer() const
Definition: MemoryBuffer.h:273
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:35
write bitcode
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
The contents of a bitcode file and its irsymtab.
Definition: IRObjectFile.h:80
section_iterator_range sections() const
Definition: ObjectFile.h:292
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:69
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:88
std::vector< BitcodeModule > Mods
Definition: IRObjectFile.h:81
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
Module.h This file contains the declarations for the Module class.
SmallVector< char, 0 > Strtab
Definition: IRObjectFile.h:82
reference get()
Returns a reference to the stored T value.
Definition: Error.h:533
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:362
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:46
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.
SmallVector< char, 0 > Symtab
Definition: IRObjectFile.h:82
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