LCOV - code coverage report
Current view: top level - lib/Object - Binary.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 24 26 92.3 %
Date: 2018-06-17 00:07:59 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Binary.cpp - A generic binary file ---------------------------------===//
       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             : // This file defines the Binary class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Object/Binary.h"
      15             : #include "llvm/ADT/StringRef.h"
      16             : #include "llvm/BinaryFormat/Magic.h"
      17             : #include "llvm/Object/Archive.h"
      18             : #include "llvm/Object/Error.h"
      19             : #include "llvm/Object/MachOUniversal.h"
      20             : #include "llvm/Object/ObjectFile.h"
      21             : #include "llvm/Object/WindowsResource.h"
      22             : #include "llvm/Support/Error.h"
      23             : #include "llvm/Support/ErrorHandling.h"
      24             : #include "llvm/Support/ErrorOr.h"
      25             : #include "llvm/Support/FileSystem.h"
      26             : #include "llvm/Support/MemoryBuffer.h"
      27             : #include <algorithm>
      28             : #include <memory>
      29             : #include <system_error>
      30             : 
      31             : using namespace llvm;
      32             : using namespace object;
      33             : 
      34             : Binary::~Binary() = default;
      35             : 
      36       22292 : Binary::Binary(unsigned int Type, MemoryBufferRef Source)
      37       22292 :     : TypeID(Type), Data(Source) {}
      38             : 
      39      362466 : StringRef Binary::getData() const { return Data.getBuffer(); }
      40             : 
      41        7246 : StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
      42             : 
      43       19434 : MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
      44             : 
      45        7464 : Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
      46             :                                                       LLVMContext *Context) {
      47        7464 :   file_magic Type = identify_magic(Buffer.getBuffer());
      48             : 
      49        7464 :   switch (Type) {
      50          84 :   case file_magic::archive:
      51         168 :     return Archive::create(Buffer);
      52        7226 :   case file_magic::elf:
      53             :   case file_magic::elf_relocatable:
      54             :   case file_magic::elf_executable:
      55             :   case file_magic::elf_shared_object:
      56             :   case file_magic::elf_core:
      57             :   case file_magic::macho_object:
      58             :   case file_magic::macho_executable:
      59             :   case file_magic::macho_fixed_virtual_memory_shared_lib:
      60             :   case file_magic::macho_core:
      61             :   case file_magic::macho_preload_executable:
      62             :   case file_magic::macho_dynamically_linked_shared_lib:
      63             :   case file_magic::macho_dynamic_linker:
      64             :   case file_magic::macho_bundle:
      65             :   case file_magic::macho_dynamically_linked_shared_lib_stub:
      66             :   case file_magic::macho_dsym_companion:
      67             :   case file_magic::macho_kext_bundle:
      68             :   case file_magic::coff_object:
      69             :   case file_magic::coff_import_library:
      70             :   case file_magic::pecoff_executable:
      71             :   case file_magic::bitcode:
      72             :   case file_magic::wasm_object:
      73       14451 :     return ObjectFile::createSymbolicFile(Buffer, Type, Context);
      74          68 :   case file_magic::macho_universal_binary:
      75         136 :     return MachOUniversalBinary::create(Buffer);
      76          38 :   case file_magic::windows_resource:
      77          76 :     return WindowsResource::createWindowsResource(Buffer);
      78             :   case file_magic::pdb:
      79             :     // PDB does not support the Binary interface.
      80           0 :     return errorCodeToError(object_error::invalid_file_type);
      81             :   case file_magic::unknown:
      82             :   case file_magic::coff_cl_gl_object:
      83             :     // Unrecognized object file format.
      84          96 :     return errorCodeToError(object_error::invalid_file_type);
      85             :   }
      86           0 :   llvm_unreachable("Unexpected Binary File Type");
      87             : }
      88             : 
      89        5515 : Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) {
      90             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      91        5515 :       MemoryBuffer::getFileOrSTDIN(Path);
      92        5515 :   if (std::error_code EC = FileOrErr.getError())
      93         140 :     return errorCodeToError(EC);
      94             :   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
      95             : 
      96             :   Expected<std::unique_ptr<Binary>> BinOrErr =
      97       10889 :       createBinary(Buffer->getMemBufferRef());
      98        5444 :   if (!BinOrErr)
      99             :     return BinOrErr.takeError();
     100             :   std::unique_ptr<Binary> &Bin = BinOrErr.get();
     101             : 
     102        5238 :   return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
     103             : }

Generated by: LCOV version 1.13