blob: 43268e63bbd2925612d75eb930db8d6e66ccaaab [file] [log] [blame]
Chris Lattnerf9bdedd2009-08-10 18:15:01 +00001//===- 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"
13using 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.
18static const struct {
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
Eric Christopher423c9e32010-05-17 21:02:07 +000037 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
38 { "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11
Eric Christopher9b006852010-05-21 21:08:52 +000039 { "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12
Eric Christopher423c9e32010-05-17 21:02:07 +000040 { "thread_local_variables", "S_THREAD_LOCAL_VARIABLES" }, // 0x13
41 { "thread_local_variable_pointers",
42 "S_THREAD_LOCAL_VARIABLE_POINTERS" }, // 0x14
43 { "thread_local_init_function_pointers",
44 "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"}, // 0x15
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000045};
46
47
48/// SectionAttrDescriptors - This is an array of descriptors for section
49/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
Kevin Enderby5440f632009-10-07 20:57:20 +000050/// by attribute, instead it is searched. The last entry has an AttrFlagEnd
51/// AttrFlag value.
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000052static const struct {
53 unsigned AttrFlag;
54 const char *AssemblerName, *EnumName;
55} SectionAttrDescriptors[] = {
56#define ENTRY(ASMNAME, ENUM) \
57 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
58ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
59ENTRY("no_toc", S_ATTR_NO_TOC)
60ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
61ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
62ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
63ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
64ENTRY("debug", S_ATTR_DEBUG)
65ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
66ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
67ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
68#undef ENTRY
Kevin Enderby5440f632009-10-07 20:57:20 +000069 { 0, "none", 0 }, // used if section has no attributes but has a stub size
70#define AttrFlagEnd 0xffffffff // non legal value, multiple attribute bits set
71 { AttrFlagEnd, 0, 0 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000072};
73
Chris Lattner74aae472010-04-08 21:26:26 +000074MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
75 unsigned TAA, unsigned reserved2, SectionKind K)
Daniel Dunbar9a744e32010-05-17 21:54:26 +000076 : MCSection(SV_MachO, K), TypeAndAttributes(TAA), Reserved2(reserved2) {
Chris Lattner74aae472010-04-08 21:26:26 +000077 assert(Segment.size() <= 16 && Section.size() <= 16 &&
78 "Segment or section string too long");
79 for (unsigned i = 0; i != 16; ++i) {
80 if (i < Segment.size())
81 SegmentName[i] = Segment[i];
82 else
83 SegmentName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +000084
Chris Lattner74aae472010-04-08 21:26:26 +000085 if (i < Section.size())
86 SectionName[i] = Section[i];
87 else
88 SectionName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +000089 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000090}
91
Chris Lattner33adcfb2009-08-22 21:43:10 +000092void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000093 raw_ostream &OS) const {
94 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
Jim Grosbachfe40f952010-10-21 22:04:05 +000095
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000096 // Get the section type and attributes.
97 unsigned TAA = getTypeAndAttributes();
98 if (TAA == 0) {
99 OS << '\n';
100 return;
101 }
102
103 OS << ',';
Jim Grosbachfe40f952010-10-21 22:04:05 +0000104
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000105 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
106 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
107 "Invalid SectionType specified!");
108
109 if (SectionTypeDescriptors[SectionType].AssemblerName)
110 OS << SectionTypeDescriptors[SectionType].AssemblerName;
111 else
112 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000113
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000114 // If we don't have any attributes, we're done.
115 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
116 if (SectionAttrs == 0) {
117 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
118 // the attribute specifier.
119 if (Reserved2 != 0)
120 OS << ",none," << Reserved2;
121 OS << '\n';
122 return;
123 }
124
125 // Check each attribute to see if we have it.
126 char Separator = ',';
127 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
128 // Check to see if we have this attribute.
129 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
130 continue;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000131
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000132 // Yep, clear it and print it.
133 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000134
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000135 OS << Separator;
136 if (SectionAttrDescriptors[i].AssemblerName)
137 OS << SectionAttrDescriptors[i].AssemblerName;
138 else
139 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
140 Separator = '+';
141 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000142
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000143 assert(SectionAttrs == 0 && "Unknown section attributes!");
Jim Grosbachfe40f952010-10-21 22:04:05 +0000144
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000145 // If we have a S_SYMBOL_STUBS size specified, print it.
146 if (Reserved2 != 0)
147 OS << ',' << Reserved2;
148 OS << '\n';
149}
150
Jan Wen Voung083cf152010-10-04 17:32:41 +0000151bool MCSectionMachO::UseCodeAlign() const {
152 return hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
153}
154
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000155bool MCSectionMachO::isVirtualSection() const {
156 return (getType() == MCSectionMachO::S_ZEROFILL ||
157 getType() == MCSectionMachO::S_GB_ZEROFILL ||
158 getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
159}
160
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000161/// StripSpaces - This removes leading and trailing spaces from the StringRef.
162static void StripSpaces(StringRef &Str) {
163 while (!Str.empty() && isspace(Str[0]))
164 Str = Str.substr(1);
165 while (!Str.empty() && isspace(Str.back()))
166 Str = Str.substr(0, Str.size()-1);
167}
168
169/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
170/// This is a string that can appear after a .section directive in a mach-o
171/// flavored .s file. If successful, this fills in the specified Out
172/// parameters and returns an empty string. When an invalid section
173/// specifier is present, this returns a string indicating the problem.
174std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
175 StringRef &Segment, // Out.
176 StringRef &Section, // Out.
177 unsigned &TAA, // Out.
178 unsigned &StubSize) { // Out.
179 // Find the first comma.
180 std::pair<StringRef, StringRef> Comma = Spec.split(',');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000181
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000182 // If there is no comma, we fail.
183 if (Comma.second.empty())
184 return "mach-o section specifier requires a segment and section "
185 "separated by a comma";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000186
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000187 // Capture segment, remove leading and trailing whitespace.
188 Segment = Comma.first;
189 StripSpaces(Segment);
190
191 // Verify that the segment is present and not too long.
192 if (Segment.empty() || Segment.size() > 16)
193 return "mach-o section specifier requires a segment whose length is "
194 "between 1 and 16 characters";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000195
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000196 // Split the section name off from any attributes if present.
197 Comma = Comma.second.split(',');
198
199 // Capture section, remove leading and trailing whitespace.
200 Section = Comma.first;
201 StripSpaces(Section);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000202
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000203 // Verify that the section is present and not too long.
204 if (Section.empty() || Section.size() > 16)
205 return "mach-o section specifier requires a section whose length is "
206 "between 1 and 16 characters";
207
208 // If there is no comma after the section, we're done.
209 TAA = 0;
210 StubSize = 0;
211 if (Comma.second.empty())
212 return "";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000213
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000214 // Otherwise, we need to parse the section type and attributes.
215 Comma = Comma.second.split(',');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000216
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000217 // Get the section type.
218 StringRef SectionType = Comma.first;
219 StripSpaces(SectionType);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000220
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000221 // Figure out which section type it is.
222 unsigned TypeID;
223 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
224 if (SectionTypeDescriptors[TypeID].AssemblerName &&
225 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
226 break;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000227
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000228 // If we didn't find the section type, reject it.
229 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
230 return "mach-o section specifier uses an unknown section type";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000231
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000232 // Remember the TypeID.
233 TAA = TypeID;
234
235 // If we have no comma after the section type, there are no attributes.
236 if (Comma.second.empty()) {
237 // S_SYMBOL_STUBS always require a symbol stub size specifier.
238 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
239 return "mach-o section specifier of type 'symbol_stubs' requires a size "
240 "specifier";
241 return "";
242 }
243
244 // Otherwise, we do have some attributes. Split off the size specifier if
245 // present.
246 Comma = Comma.second.split(',');
247 StringRef Attrs = Comma.first;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000248
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000249 // The attribute list is a '+' separated list of attributes.
250 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000251
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000252 while (1) {
253 StringRef Attr = Plus.first;
254 StripSpaces(Attr);
255
256 // Look up the attribute.
257 for (unsigned i = 0; ; ++i) {
Kevin Enderby5440f632009-10-07 20:57:20 +0000258 if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000259 return "mach-o section specifier has invalid attribute";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000260
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000261 if (SectionAttrDescriptors[i].AssemblerName &&
262 Attr == SectionAttrDescriptors[i].AssemblerName) {
263 TAA |= SectionAttrDescriptors[i].AttrFlag;
264 break;
265 }
266 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000267
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000268 if (Plus.second.empty()) break;
269 Plus = Plus.second.split('+');
270 };
271
272 // Okay, we've parsed the section attributes, see if we have a stub size spec.
273 if (Comma.second.empty()) {
274 // S_SYMBOL_STUBS always require a symbol stub size specifier.
275 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
276 return "mach-o section specifier of type 'symbol_stubs' requires a size "
277 "specifier";
278 return "";
279 }
280
281 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
282 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
283 return "mach-o section specifier cannot have a stub size specified because "
284 "it does not have type 'symbol_stubs'";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000285
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000286 // Okay, if we do, it must be a number.
287 StringRef StubSizeStr = Comma.second;
288 StripSpaces(StubSizeStr);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000289
Chris Lattnerd9221d72009-09-20 06:58:54 +0000290 // Convert the stub size from a string to an integer.
291 if (StubSizeStr.getAsInteger(0, StubSize))
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000292 return "mach-o section specifier has a malformed stub size";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000293
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000294 return "";
295}