Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCSymbolELF.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCSymbolELF.cpp ---------------------------------------------===//
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
#include "llvm/MC/MCSymbolELF.h"
10
#include "llvm/BinaryFormat/ELF.h"
11
#include "llvm/MC/MCFixupKindInfo.h"
12
13
namespace llvm {
14
15
namespace {
16
enum {
17
  // Shift value for STT_* flags. 7 possible values. 3 bits.
18
  ELF_STT_Shift = 0,
19
20
  // Shift value for STB_* flags. 4 possible values, 2 bits.
21
  ELF_STB_Shift = 3,
22
23
  // Shift value for STV_* flags. 4 possible values, 2 bits.
24
  ELF_STV_Shift = 5,
25
26
  // Shift value for STO_* flags. 3 bits. All the values are between 0x20 and
27
  // 0xe0, so we shift right by 5 before storing.
28
  ELF_STO_Shift = 7,
29
30
  // One bit.
31
  ELF_IsSignature_Shift = 10,
32
33
  // One bit.
34
  ELF_WeakrefUsedInReloc_Shift = 11,
35
36
  // One bit.
37
  ELF_BindingSet_Shift = 12
38
};
39
}
40
41
2.25M
void MCSymbolELF::setBinding(unsigned Binding) const {
42
2.25M
  setIsBindingSet();
43
2.25M
  if (getType() == ELF::STT_SECTION && 
Binding != ELF::STB_LOCAL0
)
44
0
    setType(ELF::STT_NOTYPE);
45
2.25M
  unsigned Val;
46
2.25M
  switch (Binding) {
47
2.25M
  default:
48
0
    llvm_unreachable("Unsupported Binding");
49
2.25M
  case ELF::STB_LOCAL:
50
2.16M
    Val = 0;
51
2.16M
    break;
52
2.25M
  case ELF::STB_GLOBAL:
53
86.6k
    Val = 1;
54
86.6k
    break;
55
2.25M
  case ELF::STB_WEAK:
56
320
    Val = 2;
57
320
    break;
58
2.25M
  case ELF::STB_GNU_UNIQUE:
59
2
    Val = 3;
60
2
    break;
61
2.25M
  }
62
2.25M
  uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STB_Shift);
63
2.25M
  setFlags(OtherFlags | (Val << ELF_STB_Shift));
64
2.25M
}
65
66
2.38M
unsigned MCSymbolELF::getBinding() const {
67
2.38M
  if (isBindingSet()) {
68
2.36M
    uint32_t Val = (Flags >> ELF_STB_Shift) & 3;
69
2.36M
    switch (Val) {
70
2.36M
    default:
71
0
      llvm_unreachable("Invalid value");
72
2.36M
    case 0:
73
2.17M
      return ELF::STB_LOCAL;
74
2.36M
    case 1:
75
185k
      return ELF::STB_GLOBAL;
76
2.36M
    case 2:
77
896
      return ELF::STB_WEAK;
78
2.36M
    case 3:
79
4
      return ELF::STB_GNU_UNIQUE;
80
22.1k
    }
81
22.1k
  }
82
22.1k
83
22.1k
  if (isDefined())
84
17.6k
    return ELF::STB_LOCAL;
85
4.48k
  if (isUsedInReloc())
86
4.32k
    return ELF::STB_GLOBAL;
87
160
  if (isWeakrefUsedInReloc())
88
12
    return ELF::STB_WEAK;
89
148
  if (isSignature())
90
116
    return ELF::STB_LOCAL;
91
32
  return ELF::STB_GLOBAL;
92
32
}
93
94
2.17M
void MCSymbolELF::setType(unsigned Type) const {
95
2.17M
  unsigned Val;
96
2.17M
  if (Type == ELF::STT_SECTION && 
getBinding() != ELF::STB_LOCAL2.16M
)
97
0
    return;
98
2.17M
  switch (Type) {
99
2.17M
  default:
100
0
    llvm_unreachable("Unsupported Binding");
101
2.17M
  case ELF::STT_NOTYPE:
102
2.80k
    Val = 0;
103
2.80k
    break;
104
2.17M
  case ELF::STT_OBJECT:
105
1.37k
    Val = 1;
106
1.37k
    break;
107
2.17M
  case ELF::STT_FUNC:
108
4.84k
    Val = 2;
109
4.84k
    break;
110
2.17M
  case ELF::STT_SECTION:
111
2.16M
    Val = 3;
112
2.16M
    break;
113
2.17M
  case ELF::STT_COMMON:
114
0
    Val = 4;
115
0
    break;
116
2.17M
  case ELF::STT_TLS:
117
1.67k
    Val = 5;
118
1.67k
    break;
119
2.17M
  case ELF::STT_GNU_IFUNC:
120
343
    Val = 6;
121
343
    break;
122
2.17M
  }
123
2.17M
  uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STT_Shift);
124
2.17M
  setFlags(OtherFlags | (Val << ELF_STT_Shift));
125
2.17M
}
126
127
4.25M
unsigned MCSymbolELF::getType() const {
128
4.25M
  uint32_t Val = (Flags >> ELF_STT_Shift) & 7;
129
4.25M
  switch (Val) {
130
4.25M
  default:
131
0
    llvm_unreachable("Invalid value");
132
4.25M
  case 0:
133
3.69M
    return ELF::STT_NOTYPE;
134
4.25M
  case 1:
135
14.4k
    return ELF::STT_OBJECT;
136
4.25M
  case 2:
137
42.3k
    return ELF::STT_FUNC;
138
4.25M
  case 3:
139
499k
    return ELF::STT_SECTION;
140
4.25M
  case 4:
141
0
    return ELF::STT_COMMON;
142
4.25M
  case 5:
143
3.05k
    return ELF::STT_TLS;
144
4.25M
  case 6:
145
5.65k
    return ELF::STT_GNU_IFUNC;
146
4.25M
  }
147
4.25M
}
148
149
894
void MCSymbolELF::setVisibility(unsigned Visibility) {
150
894
  assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
151
894
         Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
152
894
153
894
  uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STV_Shift);
154
894
  setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
155
894
}
156
157
98.4k
unsigned MCSymbolELF::getVisibility() const {
158
98.4k
  unsigned Visibility = (Flags >> ELF_STV_Shift) & 3;
159
98.4k
  return Visibility;
160
98.4k
}
161
162
656
void MCSymbolELF::setOther(unsigned Other) {
163
656
  assert((Other & 0x1f) == 0);
164
656
  Other >>= 5;
165
656
  assert(Other <= 0x7);
166
656
  uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STO_Shift);
167
656
  setFlags(OtherFlags | (Other << ELF_STO_Shift));
168
656
}
169
170
99.9k
unsigned MCSymbolELF::getOther() const {
171
99.9k
  unsigned Other = (Flags >> ELF_STO_Shift) & 7;
172
99.9k
  return Other << 5;
173
99.9k
}
174
175
12
void MCSymbolELF::setIsWeakrefUsedInReloc() const {
176
12
  uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift);
177
12
  setFlags(OtherFlags | (1 << ELF_WeakrefUsedInReloc_Shift));
178
12
}
179
180
598k
bool MCSymbolELF::isWeakrefUsedInReloc() const {
181
598k
  return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift);
182
598k
}
183
184
640
void MCSymbolELF::setIsSignature() const {
185
640
  uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_IsSignature_Shift);
186
640
  setFlags(OtherFlags | (1 << ELF_IsSignature_Shift));
187
640
}
188
189
598k
bool MCSymbolELF::isSignature() const {
190
598k
  return getFlags() & (0x1 << ELF_IsSignature_Shift);
191
598k
}
192
193
2.25M
void MCSymbolELF::setIsBindingSet() const {
194
2.25M
  uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_BindingSet_Shift);
195
2.25M
  setFlags(OtherFlags | (1 << ELF_BindingSet_Shift));
196
2.25M
}
197
198
2.38M
bool MCSymbolELF::isBindingSet() const {
199
2.38M
  return getFlags() & (0x1 << ELF_BindingSet_Shift);
200
2.38M
}
201
}