Coverage Report

Created: 2019-07-24 05:18

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