Coverage Report

Created: 2019-07-24 05:18

/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
// 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 declares the Binary class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_OBJECT_BINARY_H
14
#define LLVM_OBJECT_BINARY_H
15
16
#include "llvm-c/Types.h"
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_Minidump,
48
49
    ID_WinRes, // Windows resource (.res) file.
50
51
    // Object and children.
52
    ID_StartObjects,
53
    ID_COFF,
54
55
    ID_XCOFF32, // AIX XCOFF 32-bit
56
    ID_XCOFF64, // AIX XCOFF 64-bit
57
58
    ID_ELF32L, // ELF 32-bit, little endian
59
    ID_ELF32B, // ELF 32-bit, big endian
60
    ID_ELF64L, // ELF 64-bit, little endian
61
    ID_ELF64B, // ELF 64-bit, big endian
62
63
    ID_MachO32L, // MachO 32-bit, little endian
64
    ID_MachO32B, // MachO 32-bit, big endian
65
    ID_MachO64L, // MachO 64-bit, little endian
66
    ID_MachO64B, // MachO 64-bit, big endian
67
68
    ID_Wasm,
69
70
    ID_EndObjects
71
  };
72
73
58.1k
  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
74
58.1k
    if (isLE)
75
51.2k
      return is64Bits ? 
ID_ELF64L27.8k
:
ID_ELF32L23.3k
;
76
6.92k
    else
77
6.92k
      return is64Bits ? 
ID_ELF64B912
:
ID_ELF32B6.01k
;
78
58.1k
  }
79
80
348k
  static unsigned int getMachOType(bool isLE, bool is64Bits) {
81
348k
    if (isLE)
82
175k
      return is64Bits ? 
ID_MachO64L175k
:
ID_MachO32L540
;
83
173k
    else
84
173k
      return is64Bits ? 
ID_MachO64B173k
:
ID_MachO32B4
;
85
348k
  }
86
87
public:
88
  Binary() = delete;
89
  Binary(const Binary &other) = delete;
90
  virtual ~Binary();
91
92
  StringRef getData() const;
93
  StringRef getFileName() const;
94
  MemoryBufferRef getMemoryBufferRef() const;
95
96
  // Cast methods.
97
380k
  unsigned int getType() const { return TypeID; }
98
99
  // Convenience methods
100
11.4k
  bool isObject() const {
101
11.4k
    return TypeID > ID_StartObjects && 
TypeID < ID_EndObjects11.1k
;
102
11.4k
  }
103
104
  bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
105
106
8.53k
  bool isArchive() const {
107
8.53k
    return TypeID == ID_Archive;
108
8.53k
  }
109
110
5.21k
  bool isMachOUniversalBinary() const {
111
5.21k
    return TypeID == ID_MachOUniversalBinary;
112
5.21k
  }
113
114
74.4k
  bool isELF() const {
115
74.4k
    return TypeID >= ID_ELF32L && 
TypeID <= ID_ELF64B69.9k
;
116
74.4k
  }
117
118
43.2k
  bool isMachO() const {
119
43.2k
    return TypeID >= ID_MachO32L && 
TypeID <= ID_MachO64B13.9k
;
120
43.2k
  }
121
122
21.5k
  bool isCOFF() const {
123
21.5k
    return TypeID == ID_COFF;
124
21.5k
  }
125
126
  bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
127
128
1.87k
  bool isWasm() const { return TypeID == ID_Wasm; }
129
130
  bool isCOFFImportFile() const {
131
    return TypeID == ID_COFFImportFile;
132
  }
133
134
  bool isIR() const {
135
    return TypeID == ID_IR;
136
  }
137
138
  bool isMinidump() const { return TypeID == ID_Minidump; }
139
140
224k
  bool isLittleEndian() const {
141
224k
    return !(TypeID == ID_ELF32B || 
TypeID == ID_ELF64B224k
||
142
224k
             
TypeID == ID_MachO32B224k
||
TypeID == ID_MachO64B224k
);
143
224k
  }
144
145
  bool isWinRes() const { return TypeID == ID_WinRes; }
146
147
19
  Triple::ObjectFormatType getTripleObjectFormat() const {
148
19
    if (isCOFF())
149
0
      return Triple::COFF;
150
19
    if (isMachO())
151
16
      return Triple::MachO;
152
3
    if (isELF())
153
3
      return Triple::ELF;
154
0
    return Triple::UnknownObjectFormat;
155
0
  }
156
157
  static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
158
35.3k
                                     const uint64_t Size) {
159
35.3k
    if (Addr + Size < Addr || Addr + Size < Size ||
160
35.3k
        Addr + Size > uintptr_t(M.getBufferEnd()) ||
161
35.3k
        
Addr < uintptr_t(M.getBufferStart())35.3k
) {
162
19
      return object_error::unexpected_eof;
163
19
    }
164
35.3k
    return std::error_code();
165
35.3k
  }
166
};
167
168
// Create wrappers for C Binding types (see CBindingWrapping.h).
169
DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
170
171
/// Create a Binary from Source, autodetecting the file type.
172
///
173
/// @param Source The data to create the Binary from.
174
Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
175
                                               LLVMContext *Context = nullptr);
176
177
template <typename T> class OwningBinary {
178
  std::unique_ptr<T> Bin;
179
  std::unique_ptr<MemoryBuffer> Buf;
180
181
public:
182
285
  OwningBinary();
183
  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
184
  OwningBinary(OwningBinary<T>&& Other);
185
  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
186
187
  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
188
189
  T* getBinary();
190
  const T* getBinary() const;
191
};
192
193
template <typename T>
194
OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
195
                              std::unique_ptr<MemoryBuffer> Buf)
196
8.76k
    : 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
196
8.58k
    : 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
196
189
    : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
197
198
template <typename T> OwningBinary<T>::OwningBinary() = default;
199
200
template <typename T>
201
OwningBinary<T>::OwningBinary(OwningBinary &&Other)
202
16.6k
    : 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
202
16.1k
    : 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
202
477
    : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
203
204
template <typename T>
205
12
OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
206
12
  Bin = std::move(Other.Bin);
207
12
  Buf = std::move(Other.Buf);
208
12
  return *this;
209
12
}
210
211
template <typename T>
212
std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
213
OwningBinary<T>::takeBinary() {
214
  return std::make_pair(std::move(Bin), std::move(Buf));
215
}
216
217
564
template <typename T> T* OwningBinary<T>::getBinary() {
218
564
  return Bin.get();
219
564
}
220
221
template <typename T> const T* OwningBinary<T>::getBinary() const {
222
  return Bin.get();
223
}
224
225
Expected<OwningBinary<Binary>> createBinary(StringRef Path);
226
227
} // end namespace object
228
229
} // end namespace llvm
230
231
#endif // LLVM_OBJECT_BINARY_H