Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/Binary.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Binary.cpp - A generic binary file ---------------------------------===//
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
// This file defines the Binary class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Object/Binary.h"
14
#include "llvm/ADT/StringRef.h"
15
#include "llvm/BinaryFormat/Magic.h"
16
#include "llvm/Object/Archive.h"
17
#include "llvm/Object/Error.h"
18
#include "llvm/Object/MachOUniversal.h"
19
#include "llvm/Object/Minidump.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
31.9k
Binary::~Binary() = default;
35
36
Binary::Binary(unsigned int Type, MemoryBufferRef Source)
37
32.2k
    : TypeID(Type), Data(Source) {}
38
39
531k
StringRef Binary::getData() const { return Data.getBuffer(); }
40
41
20.5k
StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
42
43
17.2k
MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
44
45
Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
46
12.1k
                                                      LLVMContext *Context) {
47
12.1k
  file_magic Type = identify_magic(Buffer.getBuffer());
48
12.1k
49
12.1k
  switch (Type) {
50
12.1k
  case file_magic::archive:
51
189
    return Archive::create(Buffer);
52
12.1k
  case file_magic::elf:
53
11.7k
  case file_magic::elf_relocatable:
54
11.7k
  case file_magic::elf_executable:
55
11.7k
  case file_magic::elf_shared_object:
56
11.7k
  case file_magic::elf_core:
57
11.7k
  case file_magic::macho_object:
58
11.7k
  case file_magic::macho_executable:
59
11.7k
  case file_magic::macho_fixed_virtual_memory_shared_lib:
60
11.7k
  case file_magic::macho_core:
61
11.7k
  case file_magic::macho_preload_executable:
62
11.7k
  case file_magic::macho_dynamically_linked_shared_lib:
63
11.7k
  case file_magic::macho_dynamic_linker:
64
11.7k
  case file_magic::macho_bundle:
65
11.7k
  case file_magic::macho_dynamically_linked_shared_lib_stub:
66
11.7k
  case file_magic::macho_dsym_companion:
67
11.7k
  case file_magic::macho_kext_bundle:
68
11.7k
  case file_magic::coff_object:
69
11.7k
  case file_magic::coff_import_library:
70
11.7k
  case file_magic::pecoff_executable:
71
11.7k
  case file_magic::bitcode:
72
11.7k
  case file_magic::xcoff_object_32:
73
11.7k
  case file_magic::xcoff_object_64:
74
11.7k
  case file_magic::wasm_object:
75
11.7k
    return ObjectFile::createSymbolicFile(Buffer, Type, Context);
76
11.7k
  case file_magic::macho_universal_binary:
77
96
    return MachOUniversalBinary::create(Buffer);
78
11.7k
  case file_magic::windows_resource:
79
54
    return WindowsResource::createWindowsResource(Buffer);
80
11.7k
  case file_magic::pdb:
81
0
    // PDB does not support the Binary interface.
82
0
    return errorCodeToError(object_error::invalid_file_type);
83
11.7k
  case file_magic::unknown:
84
71
  case file_magic::coff_cl_gl_object:
85
71
    // Unrecognized object file format.
86
71
    return errorCodeToError(object_error::invalid_file_type);
87
71
  case file_magic::minidump:
88
1
    return MinidumpFile::create(Buffer);
89
0
  }
90
0
  llvm_unreachable("Unexpected Binary File Type");
91
0
}
92
93
8.82k
Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) {
94
8.82k
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
95
8.82k
      MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
96
8.82k
                                   /*RequiresNullTerminator=*/false);
97
8.82k
  if (std::error_code EC = FileOrErr.getError())
98
27
    return errorCodeToError(EC);
99
8.80k
  std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
100
8.80k
101
8.80k
  Expected<std::unique_ptr<Binary>> BinOrErr =
102
8.80k
      createBinary(Buffer->getMemBufferRef());
103
8.80k
  if (!BinOrErr)
104
219
    return BinOrErr.takeError();
105
8.58k
  std::unique_ptr<Binary> &Bin = BinOrErr.get();
106
8.58k
107
8.58k
  return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
108
8.58k
}