Coverage Report

Created: 2018-12-11 17:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/Binary.h
Line
Count
Source (jump to first uncovered line)
1
//===- Binary.h - A generic binary file -------------------------*- 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
// This file declares the Binary class.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_OBJECT_BINARY_H
15
#define LLVM_OBJECT_BINARY_H
16
17
#include "llvm/ADT/Triple.h"
18
#include "llvm/Object/Error.h"
19
#include "llvm/Support/Error.h"
20
#include "llvm/Support/MemoryBuffer.h"
21
#include <algorithm>
22
#include <memory>
23
#include <utility>
24
25
namespace llvm {
26
27
class LLVMContext;
28
class StringRef;
29
30
namespace object {
31
32
class Binary {
33
private:
34
  unsigned int TypeID;
35
36
protected:
37
  MemoryBufferRef Data;
38
39
  Binary(unsigned int Type, MemoryBufferRef Source);
40
41
  enum {
42
    ID_Archive,
43
    ID_MachOUniversalBinary,
44
    ID_COFFImportFile,
45
    ID_IR,                 // LLVM IR
46
47
    ID_WinRes, // Windows resource (.res) file.
48
49
    // Object and children.
50
    ID_StartObjects,
51
    ID_COFF,
52
53
    ID_ELF32L, // ELF 32-bit, little endian
54
    ID_ELF32B, // ELF 32-bit, big endian
55
    ID_ELF64L, // ELF 64-bit, little endian
56
    ID_ELF64B, // ELF 64-bit, big endian
57
58
    ID_MachO32L, // MachO 32-bit, little endian
59
    ID_MachO32B, // MachO 32-bit, big endian
60
    ID_MachO64L, // MachO 64-bit, little endian
61
    ID_MachO64B, // MachO 64-bit, big endian
62
63
    ID_Wasm,
64
65
    ID_EndObjects
66
  };
67
68
39.7k
  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
69
39.7k
    if (isLE)
70
35.2k
      return is64Bits ? 
ID_ELF64L17.7k
:
ID_ELF32L17.5k
;
71
4.43k
    else
72
4.43k
      return is64Bits ? 
ID_ELF64B885
:
ID_ELF32B3.54k
;
73
39.7k
  }
74
75
331k
  static unsigned int getMachOType(bool isLE, bool is64Bits) {
76
331k
    if (isLE)
77
167k
      return is64Bits ? 
ID_MachO64L166k
:
ID_MachO32L494
;
78
164k
    else
79
164k
      return is64Bits ? 
ID_MachO64B164k
:
ID_MachO32B9
;
80
331k
  }
81
82
public:
83
  Binary() = delete;
84
  Binary(const Binary &other) = delete;
85
  virtual ~Binary();
86
87
  StringRef getData() const;
88
  StringRef getFileName() const;
89
  MemoryBufferRef getMemoryBufferRef() const;
90
91
  // Cast methods.
92
352k
  unsigned int getType() const { return TypeID; }
93
94
  // Convenience methods
95
8.70k
  bool isObject() const {
96
8.70k
    return TypeID > ID_StartObjects && 
TypeID < ID_EndObjects8.47k
;
97
8.70k
  }
98
99
  bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
100
101
  bool isArchive() const {
102
    return TypeID == ID_Archive;
103
  }
104
105
3.94k
  bool isMachOUniversalBinary() const {
106
3.94k
    return TypeID == ID_MachOUniversalBinary;
107
3.94k
  }
108
109
9.38M
  bool isELF() const {
110
9.38M
    return TypeID >= ID_ELF32L && 
TypeID <= ID_ELF64B9.07M
;
111
9.38M
  }
112
113
35.0k
  bool isMachO() const {
114
35.0k
    return TypeID >= ID_MachO32L && 
TypeID <= ID_MachO64B13.4k
;
115
35.0k
  }
116
117
15.3k
  bool isCOFF() const {
118
15.3k
    return TypeID == ID_COFF;
119
15.3k
  }
120
121
  bool isWasm() const { return TypeID == ID_Wasm; }
122
123
  bool isCOFFImportFile() const {
124
    return TypeID == ID_COFFImportFile;
125
  }
126
127
  bool isIR() const {
128
    return TypeID == ID_IR;
129
  }
130
131
274k
  bool isLittleEndian() const {
132
274k
    return !(TypeID == ID_ELF32B || 
TypeID == ID_ELF64B274k
||
133
274k
             
TypeID == ID_MachO32B274k
||
TypeID == ID_MachO64B274k
);
134
274k
  }
135
136
  bool isWinRes() const { return TypeID == ID_WinRes; }
137
138
9
  Triple::ObjectFormatType getTripleObjectFormat() const {
139
9
    if (isCOFF())
140
0
      return Triple::COFF;
141
9
    if (isMachO())
142
6
      return Triple::MachO;
143
3
    if (isELF())
144
3
      return Triple::ELF;
145
0
    return Triple::UnknownObjectFormat;
146
0
  }
147
148
  static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
149
27.9k
                                     const uint64_t Size) {
150
27.9k
    if (Addr + Size < Addr || Addr + Size < Size ||
151
27.9k
        Addr + Size > uintptr_t(M.getBufferEnd()) ||
152
27.9k
        
Addr < uintptr_t(M.getBufferStart())27.9k
) {
153
16
      return object_error::unexpected_eof;
154
16
    }
155
27.9k
    return std::error_code();
156
27.9k
  }
157
};
158
159
/// Create a Binary from Source, autodetecting the file type.
160
///
161
/// @param Source The data to create the Binary from.
162
Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
163
                                               LLVMContext *Context = nullptr);
164
165
template <typename T> class OwningBinary {
166
  std::unique_ptr<T> Bin;
167
  std::unique_ptr<MemoryBuffer> Buf;
168
169
public:
170
  OwningBinary();
171
  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
172
  OwningBinary(OwningBinary<T>&& Other);
173
  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
174
175
  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
176
177
  T* getBinary();
178
  const T* getBinary() const;
179
};
180
181
template <typename T>
182
OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
183
                              std::unique_ptr<MemoryBuffer> Buf)
184
6.38k
    : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
llvm::object::OwningBinary<llvm::object::Binary>::OwningBinary(std::__1::unique_ptr<llvm::object::Binary, std::__1::default_delete<llvm::object::Binary> >, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> >)
Line
Count
Source
184
6.20k
    : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
llvm::object::OwningBinary<llvm::object::ObjectFile>::OwningBinary(std::__1::unique_ptr<llvm::object::ObjectFile, std::__1::default_delete<llvm::object::ObjectFile> >, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> >)
Line
Count
Source
184
181
    : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
185
186
template <typename T> OwningBinary<T>::OwningBinary() = default;
187
188
template <typename T>
189
OwningBinary<T>::OwningBinary(OwningBinary &&Other)
190
7.15k
    : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
llvm::object::OwningBinary<llvm::object::Binary>::OwningBinary(llvm::object::OwningBinary<llvm::object::Binary>&&)
Line
Count
Source
190
6.72k
    : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
llvm::object::OwningBinary<llvm::object::ObjectFile>::OwningBinary(llvm::object::OwningBinary<llvm::object::ObjectFile>&&)
Line
Count
Source
190
430
    : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
191
192
template <typename T>
193
OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
194
  Bin = std::move(Other.Bin);
195
  Buf = std::move(Other.Buf);
196
  return *this;
197
}
198
199
template <typename T>
200
std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
201
OwningBinary<T>::takeBinary() {
202
  return std::make_pair(std::move(Bin), std::move(Buf));
203
}
204
205
template <typename T> T* OwningBinary<T>::getBinary() {
206
  return Bin.get();
207
}
208
209
template <typename T> const T* OwningBinary<T>::getBinary() const {
210
  return Bin.get();
211
}
212
213
Expected<OwningBinary<Binary>> createBinary(StringRef Path);
214
215
} // end namespace object
216
217
} // end namespace llvm
218
219
#endif // LLVM_OBJECT_BINARY_H