Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h ------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
11
#define LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
12
13
#include "MachONormalizedFile.h"
14
#include "lld/Core/Error.h"
15
#include "lld/Core/LLVM.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/BinaryFormat/MachO.h"
18
#include "llvm/Support/Casting.h"
19
#include "llvm/Support/Endian.h"
20
#include "llvm/Support/ErrorHandling.h"
21
#include "llvm/Support/Host.h"
22
#include "llvm/Support/LEB128.h"
23
#include <system_error>
24
25
namespace lld {
26
namespace mach_o {
27
namespace normalized {
28
29
class ByteBuffer {
30
public:
31
1.81k
  ByteBuffer() : _ostream(_bytes) { }
32
33
4.57k
  void append_byte(uint8_t b) {
34
4.57k
    _ostream << b;
35
4.57k
  }
36
929
  void append_uleb128(uint64_t value) {
37
929
    llvm::encodeULEB128(value, _ostream);
38
929
  }
39
0
  void append_uleb128Fixed(uint64_t value, unsigned byteCount) {
40
0
    unsigned min = llvm::getULEB128Size(value);
41
0
    assert(min <= byteCount);
42
0
    unsigned pad = byteCount - min;
43
0
    llvm::encodeULEB128(value, _ostream, pad);
44
0
  }
45
0
  void append_sleb128(int64_t value) {
46
0
    llvm::encodeSLEB128(value, _ostream);
47
0
  }
48
331
  void append_string(StringRef str) {
49
331
    _ostream << str;
50
331
    append_byte(0);
51
331
  }
52
662
  void align(unsigned alignment) {
53
3.45k
    while ( (_ostream.tell() % alignment) != 0 )
54
2.79k
      append_byte(0);
55
662
  }
56
2.02k
  size_t size() {
57
2.02k
    return _ostream.tell();
58
2.02k
  }
59
636
  const uint8_t *bytes() {
60
636
    return reinterpret_cast<const uint8_t*>(_ostream.str().data());
61
636
  }
62
63
private:
64
  SmallVector<char, 128>        _bytes;
65
  // Stream ivar must be after SmallVector ivar to construct properly.
66
  llvm::raw_svector_ostream     _ostream;
67
};
68
69
using namespace llvm::support::endian;
70
using llvm::sys::getSwappedBytes;
71
72
template<typename T>
73
20
static inline uint16_t read16(const T *loc, bool isBig) {
74
20
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
75
20
  return isBig ? 
read16be(loc)0
:
read16le(loc)20
;
76
20
}
MachONormalizedFileBinaryReader.cpp:unsigned short lld::mach_o::normalized::read16<unsigned char>(unsigned char const*, bool)
Line
Count
Source
73
10
static inline uint16_t read16(const T *loc, bool isBig) {
74
10
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
75
10
  return isBig ? 
read16be(loc)0
:
read16le(loc)10
;
76
10
}
MachONormalizedFileBinaryReader.cpp:unsigned short lld::mach_o::normalized::read16<unsigned short>(unsigned short const*, bool)
Line
Count
Source
73
10
static inline uint16_t read16(const T *loc, bool isBig) {
74
10
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
75
10
  return isBig ? 
read16be(loc)0
:
read16le(loc)10
;
76
10
}
Unexecuted instantiation: ArchHandler.cpp:unsigned short lld::mach_o::normalized::read16<unsigned char>(unsigned char const*, bool)
77
78
template<typename T>
79
1.91k
static inline uint32_t read32(const T *loc, bool isBig) {
80
1.91k
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
1.91k
  return isBig ? 
read32be(loc)95
:
read32le(loc)1.81k
;
82
1.91k
}
ArchHandler.cpp:unsigned int lld::mach_o::normalized::read32<unsigned char>(unsigned char const*, bool)
Line
Count
Source
79
140
static inline uint32_t read32(const T *loc, bool isBig) {
80
140
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
140
  return isBig ? 
read32be(loc)0
:
read32le(loc)140
;
82
140
}
Unexecuted instantiation: ArchHandler.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: ArchHandler_arm.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: ArchHandler_arm64.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: ArchHandler_x86.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: ArchHandler_x86_64.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
CompactUnwindPass.cpp:unsigned int lld::mach_o::normalized::read32<unsigned char>(unsigned char const*, bool)
Line
Count
Source
79
28
static inline uint32_t read32(const T *loc, bool isBig) {
80
28
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
28
  return isBig ? 
read32be(loc)0
:
read32le(loc)28
;
82
28
}
Unexecuted instantiation: CompactUnwindPass.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
MachONormalizedFileBinaryReader.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Line
Count
Source
79
1.49k
static inline uint32_t read32(const T *loc, bool isBig) {
80
1.49k
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
1.49k
  return isBig ? 
read32be(loc)87
:
read32le(loc)1.40k
;
82
1.49k
}
MachONormalizedFileBinaryReader.cpp:unsigned int lld::mach_o::normalized::read32<unsigned char>(unsigned char const*, bool)
Line
Count
Source
79
101
static inline uint32_t read32(const T *loc, bool isBig) {
80
101
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
101
  return isBig ? 
read32be(loc)8
:
read32le(loc)93
;
82
101
}
Unexecuted instantiation: MachONormalizedFileBinaryWriter.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: MachONormalizedFileFromAtoms.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
MachONormalizedFileToAtoms.cpp:unsigned int lld::mach_o::normalized::read32<unsigned char>(unsigned char const*, bool)
Line
Count
Source
79
145
static inline uint32_t read32(const T *loc, bool isBig) {
80
145
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
81
145
  return isBig ? 
read32be(loc)0
:
read32le(loc)145
;
82
145
}
Unexecuted instantiation: MachONormalizedFileToAtoms.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
Unexecuted instantiation: ObjCPass.cpp:unsigned int lld::mach_o::normalized::read32<unsigned int>(unsigned int const*, bool)
83
84
template<typename T>
85
156
static inline uint64_t read64(const T *loc, bool isBig) {
86
156
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
87
156
  return isBig ? 
read64be(loc)0
:
read64le(loc)156
;
88
156
}
MachONormalizedFileToAtoms.cpp:unsigned long long lld::mach_o::normalized::read64<unsigned char>(unsigned char const*, bool)
Line
Count
Source
85
30
static inline uint64_t read64(const T *loc, bool isBig) {
86
30
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
87
30
  return isBig ? 
read64be(loc)0
:
read64le(loc)30
;
88
30
}
MachONormalizedFileBinaryReader.cpp:unsigned long long lld::mach_o::normalized::read64<unsigned long long>(unsigned long long const*, bool)
Line
Count
Source
85
126
static inline uint64_t read64(const T *loc, bool isBig) {
86
126
  assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
87
126
  return isBig ? 
read64be(loc)0
:
read64le(loc)126
;
88
126
}
Unexecuted instantiation: ArchHandler.cpp:unsigned long long lld::mach_o::normalized::read64<unsigned char>(unsigned char const*, bool)
89
90
20
inline void write16(uint8_t *loc, uint16_t value, bool isBig) {
91
20
  if (isBig)
92
0
    write16be(loc, value);
93
20
  else
94
20
    write16le(loc, value);
95
20
}
96
97
137
inline void write32(uint8_t *loc, uint32_t value, bool isBig) {
98
137
  if (isBig)
99
0
    write32be(loc, value);
100
137
  else
101
137
    write32le(loc, value);
102
137
}
103
104
0
inline void write64(uint8_t *loc, uint64_t value, bool isBig) {
105
0
  if (isBig)
106
0
    write64be(loc, value);
107
0
  else
108
0
    write64le(loc, value);
109
0
}
110
111
inline uint32_t
112
bitFieldExtract(uint32_t value, bool isBigEndianBigField, uint8_t firstBit,
113
1.36k
                                                          uint8_t bitCount) {
114
1.36k
  const uint32_t mask = ((1<<bitCount)-1);
115
1.36k
  const uint8_t shift = isBigEndianBigField ? 
(32-firstBit-bitCount)403
:
firstBit965
;
116
1.36k
  return (value >> shift) & mask;
117
1.36k
}
118
119
inline void
120
bitFieldSet(uint32_t &bits, bool isBigEndianBigField, uint32_t newBits,
121
2.47k
                            uint8_t firstBit, uint8_t bitCount) {
122
2.47k
  const uint32_t mask = ((1<<bitCount)-1);
123
2.47k
  assert((newBits & mask) == newBits);
124
2.47k
  const uint8_t shift = isBigEndianBigField ? 
(32-firstBit-bitCount)740
:
firstBit1.73k
;
125
2.47k
  bits &= ~(mask << shift);
126
2.47k
  bits |= (newBits << shift);
127
2.47k
}
128
129
inline Relocation unpackRelocation(const llvm::MachO::any_relocation_info &r,
130
291
                                   bool isBigEndian) {
131
291
  uint32_t r0 = read32(&r.r_word0, isBigEndian);
132
291
  uint32_t r1 = read32(&r.r_word1, isBigEndian);
133
291
134
291
  Relocation result;
135
291
  if (
r0 & llvm::MachO::R_SCATTERED291
) {
136
87
    // scattered relocation record always laid out like big endian bit field
137
87
    result.offset     = bitFieldExtract(r0, true, 8, 24);
138
87
    result.scattered  = true;
139
87
    result.type       = (RelocationInfoType)
140
87
                        bitFieldExtract(r0, true, 4, 4);
141
87
    result.length     = bitFieldExtract(r0, true, 2, 2);
142
87
    result.pcRel      = bitFieldExtract(r0, true, 1, 1);
143
87
    result.isExtern   = false;
144
87
    result.value      = r1;
145
87
    result.symbol     = 0;
146
291
  } else {
147
204
    result.offset     = r0;
148
204
    result.scattered  = false;
149
204
    result.type       = (RelocationInfoType)
150
204
                        bitFieldExtract(r1, isBigEndian, 28, 4);
151
204
    result.length     = bitFieldExtract(r1, isBigEndian, 25, 2);
152
204
    result.pcRel      = bitFieldExtract(r1, isBigEndian, 24, 1);
153
204
    result.isExtern   = bitFieldExtract(r1, isBigEndian, 27, 1);
154
204
    result.value      = 0;
155
204
    result.symbol     = bitFieldExtract(r1, isBigEndian, 0, 24);
156
204
  }
157
291
  return result;
158
291
}
159
160
161
inline llvm::MachO::any_relocation_info
162
494
packRelocation(const Relocation &r, bool swap, bool isBigEndian) {
163
494
  uint32_t r0 = 0;
164
494
  uint32_t r1 = 0;
165
494
166
494
  if (
r.scattered494
) {
167
137
    r1 = r.value;
168
137
    bitFieldSet(r0, true, r.offset,    8, 24);
169
137
    bitFieldSet(r0, true, r.type,      4, 4);
170
137
    bitFieldSet(r0, true, r.length,    2, 2);
171
137
    bitFieldSet(r0, true, r.pcRel,     1, 1);
172
137
    bitFieldSet(r0, true, r.scattered, 0, 1); // R_SCATTERED
173
494
  } else {
174
357
    r0 = r.offset;
175
357
    bitFieldSet(r1, isBigEndian, r.type,     28, 4);
176
357
    bitFieldSet(r1, isBigEndian, r.isExtern, 27, 1);
177
357
    bitFieldSet(r1, isBigEndian, r.length,   25, 2);
178
357
    bitFieldSet(r1, isBigEndian, r.pcRel,    24, 1);
179
357
    bitFieldSet(r1, isBigEndian, r.symbol,   0,  24);
180
357
  }
181
494
182
494
  llvm::MachO::any_relocation_info result;
183
494
  result.r_word0 = swap ? 
getSwappedBytes(r0)23
:
r0471
;
184
494
  result.r_word1 = swap ? 
getSwappedBytes(r1)23
:
r1471
;
185
494
  return result;
186
494
}
187
188
172
inline StringRef getString16(const char s[16]) {
189
172
  StringRef x = s;
190
172
  if ( x.size() > 16 )
191
12
    return x.substr(0, 16);
192
172
  else
193
160
    return x;
194
172
}
195
196
1.03k
inline void setString16(StringRef str, char s[16]) {
197
1.03k
  memset(s, 0, 16);
198
1.03k
  memcpy(s, str.begin(), (str.size() > 16) ? 
160
:
str.size()1.03k
);
199
1.03k
}
200
201
// Implemented in normalizedToAtoms() and used by normalizedFromAtoms() so
202
// that the same table can be used to map mach-o sections to and from
203
// DefinedAtom::ContentType.
204
void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType,
205
                                          StringRef &segmentName,
206
                                          StringRef &sectionName,
207
                                          SectionType &sectionType,
208
                                          SectionAttr &sectionAttrs,
209
                                          bool &relocsToDefinedCanBeImplicit);
210
211
} // namespace normalized
212
} // namespace mach_o
213
} // namespace lld
214
215
#endif // LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H