LCOV - code coverage report
Current view: top level - lib/Object - ObjectFile.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 55 60 91.7 %
Date: 2018-10-20 13:21:21 Functions: 10 12 83.3 %
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       23479 : ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
      39       23479 :     : SymbolicFile(Type, Source) {}
      40             : 
      41        1538 : bool SectionRef::containsSymbol(SymbolRef S) const {
      42             :   Expected<section_iterator> SymSec = S.getSection();
      43        1538 :   if (!SymSec) {
      44             :     // TODO: Actually report errors helpfully.
      45           0 :     consumeError(SymSec.takeError());
      46           0 :     return false;
      47             :   }
      48        1538 :   return *this == **SymSec;
      49             : }
      50             : 
      51    13255319 : uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
      52    13255319 :   uint32_t Flags = getSymbolFlags(Ref);
      53    13255319 :   if (Flags & SymbolRef::SF_Undefined)
      54             :     return 0;
      55    13218509 :   if (Flags & SymbolRef::SF_Common)
      56         654 :     return getCommonSymbolSize(Ref);
      57    13217855 :   return getSymbolValueImpl(Ref);
      58             : }
      59             : 
      60        5546 : std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
      61             :                                             DataRefImpl Symb) const {
      62        5546 :   Expected<StringRef> Name = getSymbolName(Symb);
      63        5546 :   if (!Name)
      64           4 :     return errorToErrorCode(Name.takeError());
      65        5544 :   OS << *Name;
      66        5544 :   return std::error_code();
      67             : }
      68             : 
      69           0 : uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
      70             : 
      71       66928 : bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
      72       66928 :   StringRef SectName;
      73       66928 :   if (!getSectionName(Sec, SectName))
      74             :     return SectName == ".llvmbc";
      75             :   return false;
      76             : }
      77             : 
      78       15233 : bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
      79             : 
      80        7391 : section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
      81       14782 :   return section_iterator(SectionRef(Sec, this));
      82             : }
      83             : 
      84        1407 : Triple ObjectFile::makeTriple() const {
      85             :   Triple TheTriple;
      86        1407 :   auto Arch = getArch();
      87        1407 :   TheTriple.setArch(Triple::ArchType(Arch));
      88             : 
      89             :   // For ARM targets, try to use the build attributes to build determine
      90             :   // the build target. Target features are also added, but later during
      91             :   // disassembly.
      92        1407 :   if (Arch == Triple::arm || Arch == Triple::armeb)
      93          35 :     setARMSubArch(TheTriple);
      94             : 
      95             :   // TheTriple defaults to ELF, and COFF doesn't have an environment:
      96             :   // the best we can do here is indicate that it is mach-o.
      97        2814 :   if (isMachO())
      98          21 :     TheTriple.setObjectFormat(Triple::MachO);
      99             : 
     100        1407 :   if (isCOFF()) {
     101             :     const auto COFFObj = dyn_cast<COFFObjectFile>(this);
     102          62 :     if (COFFObj->getArch() == Triple::thumb)
     103          21 :       TheTriple.setTriple("thumbv7-windows");
     104             :   }
     105             : 
     106        1407 :   return TheTriple;
     107             : }
     108             : 
     109             : Expected<std::unique_ptr<ObjectFile>>
     110       10509 : ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
     111       10509 :   StringRef Data = Object.getBuffer();
     112       10509 :   if (Type == file_magic::unknown)
     113        1483 :     Type = identify_magic(Data);
     114             : 
     115       10509 :   switch (Type) {
     116             :   case file_magic::unknown:
     117             :   case file_magic::bitcode:
     118             :   case file_magic::coff_cl_gl_object:
     119             :   case file_magic::archive:
     120             :   case file_magic::macho_universal_binary:
     121             :   case file_magic::windows_resource:
     122             :   case file_magic::pdb:
     123         127 :     return errorCodeToError(object_error::invalid_file_type);
     124        6311 :   case file_magic::elf:
     125             :   case file_magic::elf_relocatable:
     126             :   case file_magic::elf_executable:
     127             :   case file_magic::elf_shared_object:
     128             :   case file_magic::elf_core:
     129        6311 :     return createELFObjectFile(Object);
     130        1785 :   case file_magic::macho_object:
     131             :   case file_magic::macho_executable:
     132             :   case file_magic::macho_fixed_virtual_memory_shared_lib:
     133             :   case file_magic::macho_core:
     134             :   case file_magic::macho_preload_executable:
     135             :   case file_magic::macho_dynamically_linked_shared_lib:
     136             :   case file_magic::macho_dynamic_linker:
     137             :   case file_magic::macho_bundle:
     138             :   case file_magic::macho_dynamically_linked_shared_lib_stub:
     139             :   case file_magic::macho_dsym_companion:
     140             :   case file_magic::macho_kext_bundle:
     141        3570 :     return createMachOObjectFile(Object);
     142        1926 :   case file_magic::coff_object:
     143             :   case file_magic::coff_import_library:
     144             :   case file_magic::pecoff_executable:
     145        3852 :     return createCOFFObjectFile(Object);
     146         360 :   case file_magic::wasm_object:
     147         718 :     return createWasmObjectFile(Object);
     148             :   }
     149           0 :   llvm_unreachable("Unexpected Object File Type");
     150             : }
     151             : 
     152             : Expected<OwningBinary<ObjectFile>>
     153         144 : ObjectFile::createObjectFile(StringRef ObjectPath) {
     154             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
     155         144 :       MemoryBuffer::getFile(ObjectPath);
     156         144 :   if (std::error_code EC = FileOrErr.getError())
     157          34 :     return errorCodeToError(EC);
     158             :   std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
     159             : 
     160             :   Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
     161         330 :       createObjectFile(Buffer->getMemBufferRef());
     162         110 :   if (Error Err = ObjOrErr.takeError())
     163             :     return std::move(Err);
     164             :   std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
     165             : 
     166          75 :   return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
     167             : }

Generated by: LCOV version 1.13