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
67 MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
68 unsigned TAA, unsigned reserved2, SectionKind K)
69 : MCSection(K), TypeAndAttributes(TAA), Reserved2(reserved2) {
70 assert(Segment.size() <= 16 && Section.size() <= 16 &&
71 "Segment or section string too long");
72 for (unsigned i = 0; i != 16; ++i) {
73 if (i < Segment.size())
74 SegmentName[i] = Segment[i];
78 if (i < Section.size())
79 SectionName[i] = Section[i];
85 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
86 raw_ostream &OS) const {
87 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
89 // Get the section type and attributes.
90 unsigned TAA = getTypeAndAttributes();
98 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
99 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
100 "Invalid SectionType specified!");
102 if (SectionTypeDescriptors[SectionType].AssemblerName)
103 OS << SectionTypeDescriptors[SectionType].AssemblerName;
105 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
107 // If we don't have any attributes, we're done.
108 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
109 if (SectionAttrs == 0) {
110 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
111 // the attribute specifier.
113 OS << ",none," << Reserved2;
118 // Check each attribute to see if we have it.
119 char Separator = ',';
120 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
121 // Check to see if we have this attribute.
122 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
125 // Yep, clear it and print it.
126 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
129 if (SectionAttrDescriptors[i].AssemblerName)
130 OS << SectionAttrDescriptors[i].AssemblerName;
132 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
136 assert(SectionAttrs == 0 && "Unknown section attributes!");
138 // If we have a S_SYMBOL_STUBS size specified, print it.
140 OS << ',' << Reserved2;
144 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
145 static void StripSpaces(StringRef &Str) {
146 while (!Str.empty() && isspace(Str[0]))
148 while (!Str.empty() && isspace(Str.back()))
149 Str = Str.substr(0, Str.size()-1);
152 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
153 /// This is a string that can appear after a .section directive in a mach-o
154 /// flavored .s file. If successful, this fills in the specified Out
155 /// parameters and returns an empty string. When an invalid section
156 /// specifier is present, this returns a string indicating the problem.
157 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
158 StringRef &Segment, // Out.
159 StringRef &Section, // Out.
160 unsigned &TAA, // Out.
161 unsigned &StubSize) { // Out.
162 // Find the first comma.
163 std::pair<StringRef, StringRef> Comma = Spec.split(',');
165 // If there is no comma, we fail.
166 if (Comma.second.empty())
167 return "mach-o section specifier requires a segment and section "
168 "separated by a comma";
170 // Capture segment, remove leading and trailing whitespace.
171 Segment = Comma.first;
172 StripSpaces(Segment);
174 // Verify that the segment is present and not too long.
175 if (Segment.empty() || Segment.size() > 16)
176 return "mach-o section specifier requires a segment whose length is "
177 "between 1 and 16 characters";
179 // Split the section name off from any attributes if present.
180 Comma = Comma.second.split(',');
182 // Capture section, remove leading and trailing whitespace.
183 Section = Comma.first;
184 StripSpaces(Section);
186 // Verify that the section is present and not too long.
187 if (Section.empty() || Section.size() > 16)
188 return "mach-o section specifier requires a section whose length is "
189 "between 1 and 16 characters";
191 // If there is no comma after the section, we're done.
194 if (Comma.second.empty())
197 // Otherwise, we need to parse the section type and attributes.
198 Comma = Comma.second.split(',');
200 // Get the section type.
201 StringRef SectionType = Comma.first;
202 StripSpaces(SectionType);
204 // Figure out which section type it is.
206 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
207 if (SectionTypeDescriptors[TypeID].AssemblerName &&
208 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
211 // If we didn't find the section type, reject it.
212 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
213 return "mach-o section specifier uses an unknown section type";
215 // Remember the TypeID.
218 // If we have no comma after the section type, there are no attributes.
219 if (Comma.second.empty()) {
220 // S_SYMBOL_STUBS always require a symbol stub size specifier.
221 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
222 return "mach-o section specifier of type 'symbol_stubs' requires a size "
227 // Otherwise, we do have some attributes. Split off the size specifier if
229 Comma = Comma.second.split(',');
230 StringRef Attrs = Comma.first;
232 // The attribute list is a '+' separated list of attributes.
233 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
236 StringRef Attr = Plus.first;
239 // Look up the attribute.
240 for (unsigned i = 0; ; ++i) {
241 if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
242 return "mach-o section specifier has invalid attribute";
244 if (SectionAttrDescriptors[i].AssemblerName &&
245 Attr == SectionAttrDescriptors[i].AssemblerName) {
246 TAA |= SectionAttrDescriptors[i].AttrFlag;
251 if (Plus.second.empty()) break;
252 Plus = Plus.second.split('+');
255 // Okay, we've parsed the section attributes, see if we have a stub size spec.
256 if (Comma.second.empty()) {
257 // S_SYMBOL_STUBS always require a symbol stub size specifier.
258 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
259 return "mach-o section specifier of type 'symbol_stubs' requires a size "
264 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
265 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
266 return "mach-o section specifier cannot have a stub size specified because "
267 "it does not have type 'symbol_stubs'";
269 // Okay, if we do, it must be a number.
270 StringRef StubSizeStr = Comma.second;
271 StripSpaces(StubSizeStr);
273 // Convert the stub size from a string to an integer.
274 if (StubSizeStr.getAsInteger(0, StubSize))
275 return "mach-o section specifier has a malformed stub size";