LCOV - code coverage report
Current view: top level - lib/Object - ObjectFile.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 49 54 90.7 %
Date: 2017-09-14 15:23:50 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ObjectFile.cpp - File format independent object 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 a file format independent ObjectFile class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Object/ObjectFile.h"
      15             : #include "llvm/ADT/StringRef.h"
      16             : #include "llvm/BinaryFormat/Magic.h"
      17             : #include "llvm/Object/Binary.h"
      18             : #include "llvm/Object/COFF.h"
      19             : #include "llvm/Object/Error.h"
      20             : #include "llvm/Object/MachO.h"
      21             : #include "llvm/Object/Wasm.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 "llvm/Support/raw_ostream.h"
      28             : #include <algorithm>
      29             : #include <cstdint>
      30             : #include <memory>
      31             : #include <system_error>
      32             : 
      33             : using namespace llvm;
      34             : using namespace object;
      35             : 
      36           0 : void ObjectFile::anchor() {}
      37             : 
      38        6536 : ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
      39        6536 :     : SymbolicFile(Type, Source) {}
      40             : 
      41        1418 : bool SectionRef::containsSymbol(SymbolRef S) const {
      42        2836 :   Expected<section_iterator> SymSec = S.getSection();
      43        1418 :   if (!SymSec) {
      44             :     // TODO: Actually report errors helpfully.
      45           0 :     consumeError(SymSec.takeError());
      46           0 :     return false;
      47             :   }
      48        2836 :   return *this == **SymSec;
      49             : }
      50             : 
      51     8190620 : uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
      52     8190620 :   uint32_t Flags = getSymbolFlags(Ref);
      53     8190620 :   if (Flags & SymbolRef::SF_Undefined)
      54             :     return 0;
      55     8162043 :   if (Flags & SymbolRef::SF_Common)
      56          97 :     return getCommonSymbolSize(Ref);
      57     8161946 :   return getSymbolValueImpl(Ref);
      58             : }
      59             : 
      60        1662 : std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
      61             :                                             DataRefImpl Symb) const {
      62        3324 :   Expected<StringRef> Name = getSymbolName(Symb);
      63        1662 :   if (!Name)
      64           4 :     return errorToErrorCode(Name.takeError());
      65        1660 :   OS << *Name;
      66        1660 :   return std::error_code();
      67             : }
      68             : 
      69           0 : uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
      70             : 
      71       67716 : bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
      72       67716 :   StringRef SectName;
      73       67716 :   if (!getSectionName(Sec, SectName))
      74       67716 :     return SectName == ".llvmbc";
      75             :   return false;
      76             : }
      77             : 
      78        5130 : section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
      79       10260 :   return section_iterator(SectionRef(Sec, this));
      80             : }
      81             : 
      82             : Expected<std::unique_ptr<ObjectFile>>
      83        6457 : ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
      84        6457 :   StringRef Data = Object.getBuffer();
      85        6457 :   if (Type == file_magic::unknown)
      86        1112 :     Type = identify_magic(Data);
      87             : 
      88        6457 :   switch (Type) {
      89         120 :   case file_magic::unknown:
      90             :   case file_magic::bitcode:
      91             :   case file_magic::coff_cl_gl_object:
      92             :   case file_magic::archive:
      93             :   case file_magic::macho_universal_binary:
      94             :   case file_magic::windows_resource:
      95         360 :     return errorCodeToError(object_error::invalid_file_type);
      96        3723 :   case file_magic::elf:
      97             :   case file_magic::elf_relocatable:
      98             :   case file_magic::elf_executable:
      99             :   case file_magic::elf_shared_object:
     100             :   case file_magic::elf_core:
     101        7446 :     return errorOrToExpected(createELFObjectFile(Object));
     102        1468 :   case file_magic::macho_object:
     103             :   case file_magic::macho_executable:
     104             :   case file_magic::macho_fixed_virtual_memory_shared_lib:
     105             :   case file_magic::macho_core:
     106             :   case file_magic::macho_preload_executable:
     107             :   case file_magic::macho_dynamically_linked_shared_lib:
     108             :   case file_magic::macho_dynamic_linker:
     109             :   case file_magic::macho_bundle:
     110             :   case file_magic::macho_dynamically_linked_shared_lib_stub:
     111             :   case file_magic::macho_dsym_companion:
     112             :   case file_magic::macho_kext_bundle:
     113        2936 :     return createMachOObjectFile(Object);
     114        1113 :   case file_magic::coff_object:
     115             :   case file_magic::coff_import_library:
     116             :   case file_magic::pecoff_executable:
     117        3339 :     return errorOrToExpected(createCOFFObjectFile(Object));
     118          33 :   case file_magic::wasm_object:
     119          66 :     return createWasmObjectFile(Object);
     120             :   }
     121           0 :   llvm_unreachable("Unexpected Object File Type");
     122             : }
     123             : 
     124             : Expected<OwningBinary<ObjectFile>>
     125         136 : ObjectFile::createObjectFile(StringRef ObjectPath) {
     126             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
     127         272 :       MemoryBuffer::getFile(ObjectPath);
     128         136 :   if (std::error_code EC = FileOrErr.getError())
     129          93 :     return errorCodeToError(EC);
     130         210 :   std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
     131             : 
     132             :   Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
     133         315 :       createObjectFile(Buffer->getMemBufferRef());
     134         280 :   if (Error Err = ObjOrErr.takeError())
     135         140 :     return std::move(Err);
     136         140 :   std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
     137             : 
     138         490 :   return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
     139             : }

Generated by: LCOV version 1.13