LCOV - code coverage report
Current view: top level - lib/Object - IRObjectFile.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 70 75 93.3 %
Date: 2017-09-14 15:23:50 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      14             : #include "llvm/Object/IRObjectFile.h"
      15             : #include "RecordStreamer.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/BinaryFormat/Magic.h"
      18             : #include "llvm/Bitcode/BitcodeReader.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"
      26             : #include "llvm/MC/MCObjectFileInfo.h"
      27             : #include "llvm/MC/MCParser/MCAsmParser.h"
      28             : #include "llvm/MC/MCParser/MCTargetAsmParser.h"
      29             : #include "llvm/MC/MCRegisterInfo.h"
      30             : #include "llvm/MC/MCSubtargetInfo.h"
      31             : #include "llvm/Object/ObjectFile.h"
      32             : #include "llvm/Support/MemoryBuffer.h"
      33             : #include "llvm/Support/SourceMgr.h"
      34             : #include "llvm/Support/TargetRegistry.h"
      35             : #include "llvm/Support/raw_ostream.h"
      36             : using namespace llvm;
      37             : using namespace object;
      38             : 
      39          44 : IRObjectFile::IRObjectFile(MemoryBufferRef Object,
      40          44 :                            std::vector<std::unique_ptr<Module>> Mods)
      41          88 :     : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
      42         221 :   for (auto &M : this->Mods)
      43          90 :     SymTab.addModule(M.get());
      44          44 : }
      45             : 
      46          88 : IRObjectFile::~IRObjectFile() {}
      47             : 
      48             : static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
      49         415 :   return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
      50             : }
      51             : 
      52         127 : void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
      53         127 :   Symb.p += sizeof(ModuleSymbolTable::Symbol);
      54         127 : }
      55             : 
      56          94 : std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
      57             :                                               DataRefImpl Symb) const {
      58         188 :   SymTab.printSymbolName(OS, getSym(Symb));
      59          94 :   return std::error_code();
      60             : }
      61             : 
      62         321 : uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
      63         642 :   return SymTab.getSymbolFlags(getSym(Symb));
      64             : }
      65             : 
      66          44 : basic_symbol_iterator IRObjectFile::symbol_begin() const {
      67          44 :   DataRefImpl Ret;
      68          88 :   Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
      69         132 :   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
      70             : }
      71             : 
      72          44 : basic_symbol_iterator IRObjectFile::symbol_end() const {
      73          44 :   DataRefImpl Ret;
      74         132 :   Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
      75          88 :                                       SymTab.symbols().size());
      76         132 :   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
      77             : }
      78             : 
      79          37 : StringRef IRObjectFile::getTargetTriple() const {
      80             :   // Each module must have the same target triple, so we arbitrarily access the
      81             :   // first one.
      82         148 :   return Mods[0]->getTargetTriple();
      83             : }
      84             : 
      85         543 : ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
      86        1629 :   for (const SectionRef &Sec : Obj.sections()) {
      87       67899 :     if (Sec.isBitcode()) {
      88           8 :       StringRef SecContents;
      89           8 :       if (std::error_code EC = Sec.getContents(SecContents))
      90           0 :         return EC;
      91          16 :       return MemoryBufferRef(SecContents, Obj.getFileName());
      92             :     }
      93             :   }
      94             : 
      95             :   return object_error::bitcode_section_not_found;
      96             : }
      97             : 
      98         770 : ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
      99         770 :   file_magic Type = identify_magic(Object.getBuffer());
     100         770 :   switch (Type) {
     101         759 :   case file_magic::bitcode:
     102             :     return Object;
     103          10 :   case file_magic::elf_relocatable:
     104             :   case file_magic::macho_object:
     105             :   case file_magic::coff_object: {
     106             :     Expected<std::unique_ptr<ObjectFile>> ObjFile =
     107          20 :         ObjectFile::createObjectFile(Object, Type);
     108          10 :     if (!ObjFile)
     109           0 :       return errorToErrorCode(ObjFile.takeError());
     110          20 :     return findBitcodeInObject(*ObjFile->get());
     111             :   }
     112           1 :   default:
     113             :     return object_error::invalid_file_type;
     114             :   }
     115             : }
     116             : 
     117             : Expected<std::unique_ptr<IRObjectFile>>
     118          44 : IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
     119          88 :   ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
     120          44 :   if (!BCOrErr)
     121           0 :     return errorCodeToError(BCOrErr.getError());
     122             : 
     123             :   Expected<std::vector<BitcodeModule>> BMsOrErr =
     124          88 :       getBitcodeModuleList(*BCOrErr);
     125          44 :   if (!BMsOrErr)
     126           0 :     return BMsOrErr.takeError();
     127             : 
     128          88 :   std::vector<std::unique_ptr<Module>> Mods;
     129         221 :   for (auto BM : *BMsOrErr) {
     130             :     Expected<std::unique_ptr<Module>> MOrErr =
     131             :         BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
     132          90 :                          /*IsImporting*/ false);
     133          45 :     if (!MOrErr)
     134           0 :       return MOrErr.takeError();
     135             : 
     136          90 :     Mods.push_back(std::move(*MOrErr));
     137             :   }
     138             : 
     139         176 :   return std::unique_ptr<IRObjectFile>(
     140         176 :       new IRObjectFile(*BCOrErr, std::move(Mods)));
     141             : }
     142             : 
     143         675 : Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
     144        1350 :   IRSymtabFile F;
     145             :   ErrorOr<MemoryBufferRef> BCOrErr =
     146        1350 :       IRObjectFile::findBitcodeInMemBuffer(MBRef);
     147         675 :   if (!BCOrErr)
     148          15 :     return errorCodeToError(BCOrErr.getError());
     149             : 
     150        1340 :   Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr);
     151         670 :   if (!BFCOrErr)
     152           3 :     return BFCOrErr.takeError();
     153             : 
     154        1338 :   Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr);
     155         669 :   if (!FCOrErr)
     156          15 :     return FCOrErr.takeError();
     157             : 
     158        1328 :   F.Mods = std::move(BFCOrErr->Mods);
     159        1328 :   F.Symtab = std::move(FCOrErr->Symtab);
     160        1328 :   F.Strtab = std::move(FCOrErr->Strtab);
     161         664 :   F.TheReader = std::move(FCOrErr->TheReader);
     162         664 :   return std::move(F);
     163             : }

Generated by: LCOV version 1.13