Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/MC/MCSectionMachO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===//
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
#include "llvm/MC/MCSectionMachO.h"
11
#include "llvm/MC/MCContext.h"
12
#include "llvm/Support/raw_ostream.h"
13
#include <cctype>
14
using namespace llvm;
15
16
/// SectionTypeDescriptors - These are strings that describe the various section
17
/// types.  This *must* be kept in order with and stay synchronized with the
18
/// section type list.
19
static constexpr struct {
20
  StringLiteral AssemblerName, EnumName;
21
} SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = {
22
    {StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00
23
    {StringLiteral(""), StringLiteral("S_ZEROFILL")},       // 0x01
24
    {StringLiteral("cstring_literals"),
25
     StringLiteral("S_CSTRING_LITERALS")}, // 0x02
26
    {StringLiteral("4byte_literals"),
27
     StringLiteral("S_4BYTE_LITERALS")}, // 0x03
28
    {StringLiteral("8byte_literals"),
29
     StringLiteral("S_8BYTE_LITERALS")}, // 0x04
30
    {StringLiteral("literal_pointers"),
31
     StringLiteral("S_LITERAL_POINTERS")}, // 0x05
32
    {StringLiteral("non_lazy_symbol_pointers"),
33
     StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06
34
    {StringLiteral("lazy_symbol_pointers"),
35
     StringLiteral("S_LAZY_SYMBOL_POINTERS")},                        // 0x07
36
    {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08
37
    {StringLiteral("mod_init_funcs"),
38
     StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09
39
    {StringLiteral("mod_term_funcs"),
40
     StringLiteral("S_MOD_TERM_FUNC_POINTERS")},                     // 0x0A
41
    {StringLiteral("coalesced"), StringLiteral("S_COALESCED")},      // 0x0B
42
    {StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C
43
    {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")},  // 0x0D
44
    {StringLiteral("16byte_literals"),
45
     StringLiteral("S_16BYTE_LITERALS")},                           // 0x0E
46
    {StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F
47
    {StringLiteral("") /*FIXME??*/,
48
     StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10
49
    {StringLiteral("thread_local_regular"),
50
     StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11
51
    {StringLiteral("thread_local_zerofill"),
52
     StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12
53
    {StringLiteral("thread_local_variables"),
54
     StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13
55
    {StringLiteral("thread_local_variable_pointers"),
56
     StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14
57
    {StringLiteral("thread_local_init_function_pointers"),
58
     StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15
59
};
60
61
/// SectionAttrDescriptors - This is an array of descriptors for section
62
/// attributes.  Unlike the SectionTypeDescriptors, this is not directly indexed
63
/// by attribute, instead it is searched.
64
static constexpr struct {
65
  unsigned AttrFlag;
66
  StringLiteral AssemblerName, EnumName;
67
} SectionAttrDescriptors[] = {
68
#define ENTRY(ASMNAME, ENUM) \
69
  { MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
70
ENTRY("pure_instructions",   S_ATTR_PURE_INSTRUCTIONS)
71
ENTRY("no_toc",              S_ATTR_NO_TOC)
72
ENTRY("strip_static_syms",   S_ATTR_STRIP_STATIC_SYMS)
73
ENTRY("no_dead_strip",       S_ATTR_NO_DEAD_STRIP)
74
ENTRY("live_support",        S_ATTR_LIVE_SUPPORT)
75
ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
76
ENTRY("debug",               S_ATTR_DEBUG)
77
ENTRY("" /*FIXME*/,          S_ATTR_SOME_INSTRUCTIONS)
78
ENTRY("" /*FIXME*/,          S_ATTR_EXT_RELOC)
79
ENTRY("" /*FIXME*/,          S_ATTR_LOC_RELOC)
80
#undef ENTRY
81
  { 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size
82
};
83
84
MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
85
                               unsigned TAA, unsigned reserved2, SectionKind K,
86
                               MCSymbol *Begin)
87
    : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA),
88
970k
      Reserved2(reserved2) {
89
970k
  assert(Segment.size() <= 16 && Section.size() <= 16 &&
90
970k
         "Segment or section string too long");
91
16.4M
  for (unsigned i = 0; 
i != 1616.4M
;
++i15.5M
) {
92
15.5M
    if (i < Segment.size())
93
6.65M
      SegmentName[i] = Segment[i];
94
15.5M
    else
95
8.86M
      SegmentName[i] = 0;
96
15.5M
97
15.5M
    if (i < Section.size())
98
12.2M
      SectionName[i] = Section[i];
99
15.5M
    else
100
3.25M
      SectionName[i] = 0;
101
15.5M
  }
102
970k
}
103
104
void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
105
                                          raw_ostream &OS,
106
6.74k
                                          const MCExpr *Subsection) const {
107
6.74k
  OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
108
6.74k
109
6.74k
  // Get the section type and attributes.
110
6.74k
  unsigned TAA = getTypeAndAttributes();
111
6.74k
  if (
TAA == 06.74k
) {
112
648
    OS << '\n';
113
648
    return;
114
648
  }
115
6.10k
116
6.10k
  MachO::SectionType SectionType = getType();
117
6.10k
  assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
118
6.10k
         "Invalid SectionType specified!");
119
6.10k
120
6.10k
  if (
!SectionTypeDescriptors[SectionType].AssemblerName.empty()6.10k
) {
121
6.10k
    OS << ',';
122
6.10k
    OS << SectionTypeDescriptors[SectionType].AssemblerName;
123
6.10k
  } else {
124
1
    // If we have no name for the attribute, stop here.
125
1
    OS << '\n';
126
1
    return;
127
1
  }
128
6.10k
129
6.10k
  // If we don't have any attributes, we're done.
130
6.10k
  unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
131
6.10k
  if (
SectionAttrs == 06.10k
) {
132
1.02k
    // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
133
1.02k
    // the attribute specifier.
134
1.02k
    if (Reserved2 != 0)
135
0
      OS << ",none," << Reserved2;
136
1.02k
    OS << '\n';
137
1.02k
    return;
138
1.02k
  }
139
5.07k
140
5.07k
  // Check each attribute to see if we have it.
141
5.07k
  char Separator = ',';
142
5.07k
  for (unsigned i = 0;
143
16.6k
       
SectionAttrs != 0 && 16.6k
SectionAttrDescriptors[i].AttrFlag11.5k
;
144
11.5k
       
++i11.5k
) {
145
11.5k
    // Check to see if we have this attribute.
146
11.5k
    if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
147
6.49k
      continue;
148
5.07k
149
5.07k
    // Yep, clear it and print it.
150
5.07k
    SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
151
5.07k
152
5.07k
    OS << Separator;
153
5.07k
    if (!SectionAttrDescriptors[i].AssemblerName.empty())
154
5.07k
      OS << SectionAttrDescriptors[i].AssemblerName;
155
5.07k
    else
156
0
      OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
157
11.5k
    Separator = '+';
158
11.5k
  }
159
5.07k
160
5.07k
  assert(SectionAttrs == 0 && "Unknown section attributes!");
161
5.07k
162
5.07k
  // If we have a S_SYMBOL_STUBS size specified, print it.
163
5.07k
  if (Reserved2 != 0)
164
157
    OS << ',' << Reserved2;
165
6.74k
  OS << '\n';
166
6.74k
}
167
168
1.13k
bool MCSectionMachO::UseCodeAlign() const {
169
1.13k
  return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
170
1.13k
}
171
172
739k
bool MCSectionMachO::isVirtualSection() const {
173
739k
  return (getType() == MachO::S_ZEROFILL ||
174
693k
          getType() == MachO::S_GB_ZEROFILL ||
175
693k
          getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
176
739k
}
177
178
/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
179
/// This is a string that can appear after a .section directive in a mach-o
180
/// flavored .s file.  If successful, this fills in the specified Out
181
/// parameters and returns an empty string.  When an invalid section
182
/// specifier is present, this returns a string indicating the problem.
183
std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec,        // In.
184
                                                  StringRef &Segment,    // Out.
185
                                                  StringRef &Section,    // Out.
186
                                                  unsigned  &TAA,        // Out.
187
                                                  bool      &TAAParsed,  // Out.
188
4.50k
                                                  unsigned  &StubSize) { // Out.
189
4.50k
  TAAParsed = false;
190
4.50k
191
4.50k
  SmallVector<StringRef, 5> SplitSpec;
192
4.50k
  Spec.split(SplitSpec, ',');
193
4.50k
  // Remove leading and trailing whitespace.
194
22.5k
  auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
195
22.5k
    return SplitSpec.size() > Idx ? 
SplitSpec[Idx].trim()13.5k
:
StringRef()9.02k
;
196
22.5k
  };
197
4.50k
  Segment = GetEmptyOrTrim(0);
198
4.50k
  Section = GetEmptyOrTrim(1);
199
4.50k
  StringRef SectionType = GetEmptyOrTrim(2);
200
4.50k
  StringRef Attrs = GetEmptyOrTrim(3);
201
4.50k
  StringRef StubSizeStr = GetEmptyOrTrim(4);
202
4.50k
203
4.50k
  // Verify that the segment is present and not too long.
204
4.50k
  if (
Segment.empty() || 4.50k
Segment.size() > 164.50k
)
205
0
    return "mach-o section specifier requires a segment whose length is "
206
0
           "between 1 and 16 characters";
207
4.50k
208
4.50k
  // Verify that the section is present and not too long.
209
4.50k
  
if (4.50k
Section.empty()4.50k
)
210
4
    return "mach-o section specifier requires a segment and section "
211
4
           "separated by a comma";
212
4.50k
213
4.50k
  
if (4.50k
Section.size() > 164.50k
)
214
0
    return "mach-o section specifier requires a section whose length is "
215
0
           "between 1 and 16 characters";
216
4.50k
217
4.50k
  // If there is no comma after the section, we're done.
218
4.50k
  TAA = 0;
219
4.50k
  StubSize = 0;
220
4.50k
  if (SectionType.empty())
221
1.34k
    return "";
222
3.15k
223
3.15k
  // Figure out which section type it is.
224
3.15k
  auto TypeDescriptor = std::find_if(
225
3.15k
      std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors),
226
9.44k
      [&](decltype(*SectionTypeDescriptors) &Descriptor) {
227
9.44k
        return SectionType == Descriptor.AssemblerName;
228
9.44k
      });
229
3.15k
230
3.15k
  // If we didn't find the section type, reject it.
231
3.15k
  if (TypeDescriptor == std::end(SectionTypeDescriptors))
232
0
    return "mach-o section specifier uses an unknown section type";
233
3.15k
234
3.15k
  // Remember the TypeID.
235
3.15k
  TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
236
3.15k
  TAAParsed = true;
237
3.15k
238
3.15k
  // If we have no comma after the section type, there are no attributes.
239
3.15k
  if (
Attrs.empty()3.15k
) {
240
1.82k
    // S_SYMBOL_STUBS always require a symbol stub size specifier.
241
1.82k
    if (TAA == MachO::S_SYMBOL_STUBS)
242
0
      return "mach-o section specifier of type 'symbol_stubs' requires a size "
243
0
             "specifier";
244
1.82k
    return "";
245
1.82k
  }
246
1.32k
247
1.32k
  // The attribute list is a '+' separated list of attributes.
248
1.32k
  SmallVector<StringRef, 1> SectionAttrs;
249
1.32k
  Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
250
1.32k
251
1.33k
  for (StringRef &SectionAttr : SectionAttrs) {
252
1.33k
    auto AttrDescriptorI = std::find_if(
253
1.33k
        std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors),
254
5.11k
        [&](decltype(*SectionAttrDescriptors) &Descriptor) {
255
5.11k
          return SectionAttr.trim() == Descriptor.AssemblerName;
256
5.11k
        });
257
1.33k
    if (AttrDescriptorI == std::end(SectionAttrDescriptors))
258
0
      return "mach-o section specifier has invalid attribute";
259
1.33k
260
1.33k
    TAA |= AttrDescriptorI->AttrFlag;
261
1.33k
  }
262
1.32k
263
1.32k
  // Okay, we've parsed the section attributes, see if we have a stub size spec.
264
1.32k
  
if (1.32k
StubSizeStr.empty()1.32k
) {
265
1.32k
    // S_SYMBOL_STUBS always require a symbol stub size specifier.
266
1.32k
    if (TAA == MachO::S_SYMBOL_STUBS)
267
0
      return "mach-o section specifier of type 'symbol_stubs' requires a size "
268
0
      "specifier";
269
1.32k
    return "";
270
1.32k
  }
271
7
272
7
  // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
273
7
  
if (7
(TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS7
)
274
0
    return "mach-o section specifier cannot have a stub size specified because "
275
0
           "it does not have type 'symbol_stubs'";
276
7
277
7
  // Convert the stub size from a string to an integer.
278
7
  
if (7
StubSizeStr.getAsInteger(0, StubSize)7
)
279
0
    return "mach-o section specifier has a malformed stub size";
280
7
281
7
  return "";
282
7
}