Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/IRObjectFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
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
// Part of the IRObjectFile class implementation.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Object/IRObjectFile.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/BinaryFormat/Magic.h"
16
#include "llvm/Bitcode/BitcodeReader.h"
17
#include "llvm/IR/GVMaterializer.h"
18
#include "llvm/IR/LLVMContext.h"
19
#include "llvm/IR/Mangler.h"
20
#include "llvm/IR/Module.h"
21
#include "llvm/Object/ObjectFile.h"
22
#include "llvm/Support/MemoryBuffer.h"
23
#include "llvm/Support/TargetRegistry.h"
24
#include "llvm/Support/raw_ostream.h"
25
using namespace llvm;
26
using namespace object;
27
28
IRObjectFile::IRObjectFile(MemoryBufferRef Object,
29
                           std::vector<std::unique_ptr<Module>> Mods)
30
65
    : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
31
65
  for (auto &M : this->Mods)
32
66
    SymTab.addModule(M.get());
33
65
}
34
35
65
IRObjectFile::~IRObjectFile() {}
36
37
538
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
38
538
  return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
39
538
}
40
41
165
void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
42
165
  Symb.p += sizeof(ModuleSymbolTable::Symbol);
43
165
}
44
45
129
Error IRObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
46
129
  SymTab.printSymbolName(OS, getSym(Symb));
47
129
  return Error::success();
48
129
}
49
50
409
uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
51
409
  return SymTab.getSymbolFlags(getSym(Symb));
52
409
}
53
54
65
basic_symbol_iterator IRObjectFile::symbol_begin() const {
55
65
  DataRefImpl Ret;
56
65
  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
57
65
  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
58
65
}
59
60
65
basic_symbol_iterator IRObjectFile::symbol_end() const {
61
65
  DataRefImpl Ret;
62
65
  Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
63
65
                                      SymTab.symbols().size());
64
65
  return basic_symbol_iterator(BasicSymbolRef(Ret, this));
65
65
}
66
67
42
StringRef IRObjectFile::getTargetTriple() const {
68
42
  // Each module must have the same target triple, so we arbitrarily access the
69
42
  // first one.
70
42
  return Mods[0]->getTargetTriple();
71
42
}
72
73
Expected<MemoryBufferRef>
74
401
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
75
67.7k
  for (const SectionRef &Sec : Obj.sections()) {
76
67.7k
    if (Sec.isBitcode()) {
77
8
      Expected<StringRef> Contents = Sec.getContents();
78
8
      if (!Contents)
79
0
        return Contents.takeError();
80
8
      if (Contents->size() <= 1)
81
1
        return errorCodeToError(object_error::bitcode_section_not_found);
82
7
      return MemoryBufferRef(*Contents, Obj.getFileName());
83
7
    }
84
67.7k
  }
85
401
86
401
  
return errorCodeToError(object_error::bitcode_section_not_found)393
;
87
401
}
88
89
Expected<MemoryBufferRef>
90
1.07k
IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
91
1.07k
  file_magic Type = identify_magic(Object.getBuffer());
92
1.07k
  switch (Type) {
93
1.07k
  case file_magic::bitcode:
94
1.06k
    return Object;
95
1.07k
  case file_magic::elf_relocatable:
96
4
  case file_magic::macho_object:
97
4
  case file_magic::coff_object: {
98
4
    Expected<std::unique_ptr<ObjectFile>> ObjFile =
99
4
        ObjectFile::createObjectFile(Object, Type);
100
4
    if (!ObjFile)
101
0
      return ObjFile.takeError();
102
4
    return findBitcodeInObject(*ObjFile->get());
103
4
  }
104
4
  default:
105
2
    return errorCodeToError(object_error::invalid_file_type);
106
1.07k
  }
107
1.07k
}
108
109
Expected<std::unique_ptr<IRObjectFile>>
110
65
IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
111
65
  Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
112
65
  if (!BCOrErr)
113
0
    return BCOrErr.takeError();
114
65
115
65
  Expected<std::vector<BitcodeModule>> BMsOrErr =
116
65
      getBitcodeModuleList(*BCOrErr);
117
65
  if (!BMsOrErr)
118
0
    return BMsOrErr.takeError();
119
65
120
65
  std::vector<std::unique_ptr<Module>> Mods;
121
66
  for (auto BM : *BMsOrErr) {
122
66
    Expected<std::unique_ptr<Module>> MOrErr =
123
66
        BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
124
66
                         /*IsImporting*/ false);
125
66
    if (!MOrErr)
126
0
      return MOrErr.takeError();
127
66
128
66
    Mods.push_back(std::move(*MOrErr));
129
66
  }
130
65
131
65
  return std::unique_ptr<IRObjectFile>(
132
65
      new IRObjectFile(*BCOrErr, std::move(Mods)));
133
65
}
134
135
942
Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
136
942
  IRSymtabFile F;
137
942
  Expected<MemoryBufferRef> BCOrErr =
138
942
      IRObjectFile::findBitcodeInMemBuffer(MBRef);
139
942
  if (!BCOrErr)
140
1
    return BCOrErr.takeError();
141
941
142
941
  Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr);
143
941
  if (!BFCOrErr)
144
0
    return BFCOrErr.takeError();
145
941
146
941
  Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr);
147
941
  if (!FCOrErr)
148
3
    return FCOrErr.takeError();
149
938
150
938
  F.Mods = std::move(BFCOrErr->Mods);
151
938
  F.Symtab = std::move(FCOrErr->Symtab);
152
938
  F.Strtab = std::move(FCOrErr->Strtab);
153
938
  F.TheReader = std::move(FCOrErr->TheReader);
154
938
  return std::move(F);
155
938
}