LLVM  10.0.0svn
MCSectionMachO.cpp
Go to the documentation of this file.
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 
10 #include "llvm/MC/MCContext.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 {
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;
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  Reserved2(reserved2) {
88  assert(Segment.size() <= 16 && Section.size() <= 16 &&
89  "Segment or section string too long");
90  for (unsigned i = 0; i != 16; ++i) {
91  if (i < Segment.size())
92  SegmentName[i] = Segment[i];
93  else
94  SegmentName[i] = 0;
95 
96  if (i < Section.size())
97  SectionName[i] = Section[i];
98  else
99  SectionName[i] = 0;
100  }
101 }
102 
104  raw_ostream &OS,
105  const MCExpr *Subsection) const {
106  OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
107 
108  // Get the section type and attributes.
109  unsigned TAA = getTypeAndAttributes();
110  if (TAA == 0) {
111  OS << '\n';
112  return;
113  }
114 
116  assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
117  "Invalid SectionType specified!");
118 
119  if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
120  OS << ',';
121  OS << SectionTypeDescriptors[SectionType].AssemblerName;
122  } else {
123  // If we have no name for the attribute, stop here.
124  OS << '\n';
125  return;
126  }
127 
128  // If we don't have any attributes, we're done.
129  unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
130  if (SectionAttrs == 0) {
131  // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
132  // the attribute specifier.
133  if (Reserved2 != 0)
134  OS << ",none," << Reserved2;
135  OS << '\n';
136  return;
137  }
138 
139  // Check each attribute to see if we have it.
140  char Separator = ',';
141  for (unsigned i = 0;
142  SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
143  ++i) {
144  // Check to see if we have this attribute.
145  if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
146  continue;
147 
148  // Yep, clear it and print it.
149  SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
150 
151  OS << Separator;
154  else
155  OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
156  Separator = '+';
157  }
158 
159  assert(SectionAttrs == 0 && "Unknown section attributes!");
160 
161  // If we have a S_SYMBOL_STUBS size specified, print it.
162  if (Reserved2 != 0)
163  OS << ',' << Reserved2;
164  OS << '\n';
165 }
166 
169 }
170 
172  return (getType() == MachO::S_ZEROFILL ||
175 }
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.
183  StringRef &Segment, // Out.
184  StringRef &Section, // Out.
185  unsigned &TAA, // Out.
186  bool &TAAParsed, // Out.
187  unsigned &StubSize) { // Out.
188  TAAParsed = false;
189 
190  SmallVector<StringRef, 5> SplitSpec;
191  Spec.split(SplitSpec, ',');
192  // Remove leading and trailing whitespace.
193  auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
194  return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
195  };
196  Segment = GetEmptyOrTrim(0);
197  Section = GetEmptyOrTrim(1);
198  StringRef SectionType = GetEmptyOrTrim(2);
199  StringRef Attrs = GetEmptyOrTrim(3);
200  StringRef StubSizeStr = GetEmptyOrTrim(4);
201 
202  // Verify that the segment is present and not too long.
203  if (Segment.empty() || Segment.size() > 16)
204  return "mach-o section specifier requires a segment whose length is "
205  "between 1 and 16 characters";
206 
207  // Verify that the section is present and not too long.
208  if (Section.empty())
209  return "mach-o section specifier requires a segment and section "
210  "separated by a comma";
211 
212  if (Section.size() > 16)
213  return "mach-o section specifier requires a section whose length is "
214  "between 1 and 16 characters";
215 
216  // If there is no comma after the section, we're done.
217  TAA = 0;
218  StubSize = 0;
219  if (SectionType.empty())
220  return "";
221 
222  // Figure out which section type it is.
223  auto TypeDescriptor = std::find_if(
225  [&](decltype(*SectionTypeDescriptors) &Descriptor) {
226  return SectionType == Descriptor.AssemblerName;
227  });
228 
229  // If we didn't find the section type, reject it.
230  if (TypeDescriptor == std::end(SectionTypeDescriptors))
231  return "mach-o section specifier uses an unknown section type";
232 
233  // Remember the TypeID.
234  TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
235  TAAParsed = true;
236 
237  // If we have no comma after the section type, there are no attributes.
238  if (Attrs.empty()) {
239  // S_SYMBOL_STUBS always require a symbol stub size specifier.
240  if (TAA == MachO::S_SYMBOL_STUBS)
241  return "mach-o section specifier of type 'symbol_stubs' requires a size "
242  "specifier";
243  return "";
244  }
245 
246  // The attribute list is a '+' separated list of attributes.
247  SmallVector<StringRef, 1> SectionAttrs;
248  Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
249 
250  for (StringRef &SectionAttr : SectionAttrs) {
251  auto AttrDescriptorI = std::find_if(
253  [&](decltype(*SectionAttrDescriptors) &Descriptor) {
254  return SectionAttr.trim() == Descriptor.AssemblerName;
255  });
256  if (AttrDescriptorI == std::end(SectionAttrDescriptors))
257  return "mach-o section specifier has invalid attribute";
258 
259  TAA |= AttrDescriptorI->AttrFlag;
260  }
261 
262  // Okay, we've parsed the section attributes, see if we have a stub size spec.
263  if (StubSizeStr.empty()) {
264  // S_SYMBOL_STUBS always require a symbol stub size specifier.
265  if (TAA == MachO::S_SYMBOL_STUBS)
266  return "mach-o section specifier of type 'symbol_stubs' requires a size "
267  "specifier";
268  return "";
269  }
270 
271  // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
273  return "mach-o section specifier cannot have a stub size specified because "
274  "it does not have type 'symbol_stubs'";
275 
276  // Convert the stub size from a string to an integer.
277  if (StubSizeStr.getAsInteger(0, StubSize))
278  return "mach-o section specifier has a malformed stub size";
279 
280  return "";
281 }
static constexpr struct @382 SectionAttrDescriptors[]
SectionAttrDescriptors - This is an array of descriptors for section attributes.
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
Definition: MachO.h:136
bool hasAttribute(unsigned Value) const
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
Definition: MachO.h:181
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section in files with the MY_DYLDLINK f...
Definition: MachO.h:187
unsigned getTypeAndAttributes() const
S_ATTR_LOC_RELOC - Section has local relocation entries.
Definition: MachO.h:206
S_ATTR_EXT_RELOC - Section has external relocation entries.
Definition: MachO.h:204
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
StringRef getSectionName() const
S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by dyld.
Definition: MachO.h:194
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
#define ENTRY(ASMNAME, ENUM)
StringLiteral AssemblerName
StringLiteral EnumName
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
StringRef getSegmentName() const
S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
Definition: MachO.h:191
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:838
static std::string ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
Parse the section specifier indicated by "Spec".
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
Definition: MachO.h:147
S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
Definition: MachO.h:161
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
bool UseCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
size_t size() const
Definition: SmallVector.h:52
MachO::SectionType getType() const
S_ATTR_NO_DEAD_STRIP - No dead stripping.
Definition: MachO.h:189
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:478
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:696
unsigned AttrFlag
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
Definition: MachO.h:202
static constexpr struct @381 SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1]
SectionTypeDescriptors - These are strings that describe the various section types.
S_ATTR_DEBUG - A debug section.
Definition: MachO.h:196
S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be in a ranlib table of contents...
Definition: MachO.h:184
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
S_ZEROFILL - Zero fill on demand section.
Definition: MachO.h:121
SectionType
These are the section type and attributes fields.
Definition: MachO.h:114
void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const override