1 //===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSectionMachO.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/Support/raw_ostream.h"
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.
19 const char *AssemblerName, *EnumName;
20 } SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
21 { "regular", "S_REGULAR" }, // 0x00
22 { 0, "S_ZEROFILL" }, // 0x01
23 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
24 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
25 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
26 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
27 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
28 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
29 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
30 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
31 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
32 { "coalesced", "S_COALESCED" }, // 0x0B
33 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
34 { "interposing", "S_INTERPOSING" }, // 0x0D
35 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
36 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
37 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10
41 /// SectionAttrDescriptors - This is an array of descriptors for section
42 /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
43 /// by attribute, instead it is searched. The last entry has an AttrFlagEnd
47 const char *AssemblerName, *EnumName;
48 } SectionAttrDescriptors[] = {
49 #define ENTRY(ASMNAME, ENUM) \
50 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
51 ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
52 ENTRY("no_toc", S_ATTR_NO_TOC)
53 ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
54 ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
55 ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
56 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
57 ENTRY("debug", S_ATTR_DEBUG)
58 ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
59 ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
60 ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
62 { 0, "none", 0 }, // used if section has no attributes but has a stub size
63 #define AttrFlagEnd 0xffffffff // non legal value, multiple attribute bits set
68 MCSectionMachO *MCSectionMachO::
69 Create(const StringRef &Segment, const StringRef &Section,
70 unsigned TypeAndAttributes, unsigned Reserved2,
71 SectionKind K, MCContext &Ctx) {
72 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero.
73 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
77 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
78 raw_ostream &OS) const {
79 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
81 // Get the section type and attributes.
82 unsigned TAA = getTypeAndAttributes();
90 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
91 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
92 "Invalid SectionType specified!");
94 if (SectionTypeDescriptors[SectionType].AssemblerName)
95 OS << SectionTypeDescriptors[SectionType].AssemblerName;
97 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
99 // If we don't have any attributes, we're done.
100 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
101 if (SectionAttrs == 0) {
102 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
103 // the attribute specifier.
105 OS << ",none," << Reserved2;
110 // Check each attribute to see if we have it.
111 char Separator = ',';
112 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
113 // Check to see if we have this attribute.
114 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
117 // Yep, clear it and print it.
118 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
121 if (SectionAttrDescriptors[i].AssemblerName)
122 OS << SectionAttrDescriptors[i].AssemblerName;
124 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
128 assert(SectionAttrs == 0 && "Unknown section attributes!");
130 // If we have a S_SYMBOL_STUBS size specified, print it.
132 OS << ',' << Reserved2;
136 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
137 static void StripSpaces(StringRef &Str) {
138 while (!Str.empty() && isspace(Str[0]))
140 while (!Str.empty() && isspace(Str.back()))
141 Str = Str.substr(0, Str.size()-1);
144 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
145 /// This is a string that can appear after a .section directive in a mach-o
146 /// flavored .s file. If successful, this fills in the specified Out
147 /// parameters and returns an empty string. When an invalid section
148 /// specifier is present, this returns a string indicating the problem.
149 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
150 StringRef &Segment, // Out.
151 StringRef &Section, // Out.
152 unsigned &TAA, // Out.
153 unsigned &StubSize) { // Out.
154 // Find the first comma.
155 std::pair<StringRef, StringRef> Comma = Spec.split(',');
157 // If there is no comma, we fail.
158 if (Comma.second.empty())
159 return "mach-o section specifier requires a segment and section "
160 "separated by a comma";
162 // Capture segment, remove leading and trailing whitespace.
163 Segment = Comma.first;
164 StripSpaces(Segment);
166 // Verify that the segment is present and not too long.
167 if (Segment.empty() || Segment.size() > 16)
168 return "mach-o section specifier requires a segment whose length is "
169 "between 1 and 16 characters";
171 // Split the section name off from any attributes if present.
172 Comma = Comma.second.split(',');
174 // Capture section, remove leading and trailing whitespace.
175 Section = Comma.first;
176 StripSpaces(Section);
178 // Verify that the section is present and not too long.
179 if (Section.empty() || Section.size() > 16)
180 return "mach-o section specifier requires a section whose length is "
181 "between 1 and 16 characters";
183 // If there is no comma after the section, we're done.
186 if (Comma.second.empty())
189 // Otherwise, we need to parse the section type and attributes.
190 Comma = Comma.second.split(',');
192 // Get the section type.
193 StringRef SectionType = Comma.first;
194 StripSpaces(SectionType);
196 // Figure out which section type it is.
198 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
199 if (SectionTypeDescriptors[TypeID].AssemblerName &&
200 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
203 // If we didn't find the section type, reject it.
204 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
205 return "mach-o section specifier uses an unknown section type";
207 // Remember the TypeID.
210 // If we have no comma after the section type, there are no attributes.
211 if (Comma.second.empty()) {
212 // S_SYMBOL_STUBS always require a symbol stub size specifier.
213 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
214 return "mach-o section specifier of type 'symbol_stubs' requires a size "
219 // Otherwise, we do have some attributes. Split off the size specifier if
221 Comma = Comma.second.split(',');
222 StringRef Attrs = Comma.first;
224 // The attribute list is a '+' separated list of attributes.
225 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
228 StringRef Attr = Plus.first;
231 // Look up the attribute.
232 for (unsigned i = 0; ; ++i) {
233 if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
234 return "mach-o section specifier has invalid attribute";
236 if (SectionAttrDescriptors[i].AssemblerName &&
237 Attr == SectionAttrDescriptors[i].AssemblerName) {
238 TAA |= SectionAttrDescriptors[i].AttrFlag;
243 if (Plus.second.empty()) break;
244 Plus = Plus.second.split('+');
247 // Okay, we've parsed the section attributes, see if we have a stub size spec.
248 if (Comma.second.empty()) {
249 // S_SYMBOL_STUBS always require a symbol stub size specifier.
250 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
251 return "mach-o section specifier of type 'symbol_stubs' requires a size "
256 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
257 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
258 return "mach-o section specifier cannot have a stub size specified because "
259 "it does not have type 'symbol_stubs'";
261 // Okay, if we do, it must be a number.
262 StringRef StubSizeStr = Comma.second;
263 StripSpaces(StubSizeStr);
265 // Convert the stub size from a string to an integer.
266 if (StubSizeStr.getAsInteger(0, StubSize))
267 return "mach-o section specifier has a malformed stub size";