blob: 577e93aed6bc6968680e40ed829817a8dff93a81 [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"
Nick Lewycky476b2422010-12-19 20:43:38 +000013#include <cctype>
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000014using namespace llvm;
15
16/// SectionTypeDescriptors - These are strings that describe the various section
17/// types. This *must* be kept in order with and stay synchronized with the
18/// section type list.
19static const struct {
20 const char *AssemblerName, *EnumName;
21} SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
22 { "regular", "S_REGULAR" }, // 0x00
23 { 0, "S_ZEROFILL" }, // 0x01
24 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
25 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
26 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
27 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
28 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
29 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
30 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
31 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
32 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
33 { "coalesced", "S_COALESCED" }, // 0x0B
34 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
35 { "interposing", "S_INTERPOSING" }, // 0x0D
36 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
37 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
Eric Christopher423c9e32010-05-17 21:02:07 +000038 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
39 { "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11
Eric Christopher9b006852010-05-21 21:08:52 +000040 { "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12
Eric Christopher423c9e32010-05-17 21:02:07 +000041 { "thread_local_variables", "S_THREAD_LOCAL_VARIABLES" }, // 0x13
42 { "thread_local_variable_pointers",
43 "S_THREAD_LOCAL_VARIABLE_POINTERS" }, // 0x14
44 { "thread_local_init_function_pointers",
45 "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"}, // 0x15
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000046};
47
48
49/// SectionAttrDescriptors - This is an array of descriptors for section
50/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
Kevin Enderby5440f632009-10-07 20:57:20 +000051/// by attribute, instead it is searched. The last entry has an AttrFlagEnd
52/// AttrFlag value.
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000053static const struct {
54 unsigned AttrFlag;
55 const char *AssemblerName, *EnumName;
56} SectionAttrDescriptors[] = {
57#define ENTRY(ASMNAME, ENUM) \
58 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
59ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
60ENTRY("no_toc", S_ATTR_NO_TOC)
61ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
62ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
63ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
64ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
65ENTRY("debug", S_ATTR_DEBUG)
66ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
67ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
68ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
69#undef ENTRY
Kevin Enderby5440f632009-10-07 20:57:20 +000070 { 0, "none", 0 }, // used if section has no attributes but has a stub size
71#define AttrFlagEnd 0xffffffff // non legal value, multiple attribute bits set
72 { AttrFlagEnd, 0, 0 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000073};
74
Chris Lattner74aae472010-04-08 21:26:26 +000075MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
76 unsigned TAA, unsigned reserved2, SectionKind K)
Daniel Dunbar9a744e32010-05-17 21:54:26 +000077 : MCSection(SV_MachO, K), TypeAndAttributes(TAA), Reserved2(reserved2) {
Chris Lattner74aae472010-04-08 21:26:26 +000078 assert(Segment.size() <= 16 && Section.size() <= 16 &&
79 "Segment or section string too long");
80 for (unsigned i = 0; i != 16; ++i) {
81 if (i < Segment.size())
82 SegmentName[i] = Segment[i];
83 else
84 SegmentName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +000085
Chris Lattner74aae472010-04-08 21:26:26 +000086 if (i < Section.size())
87 SectionName[i] = Section[i];
88 else
89 SectionName[i] = 0;
Jim Grosbachfe40f952010-10-21 22:04:05 +000090 }
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000091}
92
Chris Lattner33adcfb2009-08-22 21:43:10 +000093void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000094 raw_ostream &OS) const {
95 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
Jim Grosbachfe40f952010-10-21 22:04:05 +000096
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000097 // Get the section type and attributes.
98 unsigned TAA = getTypeAndAttributes();
99 if (TAA == 0) {
100 OS << '\n';
101 return;
102 }
103
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000104 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
105 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
106 "Invalid SectionType specified!");
107
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000108 if (SectionTypeDescriptors[SectionType].AssemblerName) {
109 OS << ',';
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000110 OS << SectionTypeDescriptors[SectionType].AssemblerName;
Stuart Hastings37c79c52011-02-21 21:07:07 +0000111 } else {
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000112 // If we have no name for the attribute, stop here.
Stuart Hastings37c79c52011-02-21 21:07:07 +0000113 OS << '\n';
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000114 return;
Stuart Hastings37c79c52011-02-21 21:07:07 +0000115 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000116
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000117 // If we don't have any attributes, we're done.
118 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
119 if (SectionAttrs == 0) {
120 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
121 // the attribute specifier.
122 if (Reserved2 != 0)
123 OS << ",none," << Reserved2;
124 OS << '\n';
125 return;
126 }
127
128 // Check each attribute to see if we have it.
129 char Separator = ',';
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000130 for (unsigned i = 0;
131 SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
132 ++i) {
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000133 // Check to see if we have this attribute.
134 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
135 continue;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000136
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000137 // Yep, clear it and print it.
138 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000139
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000140 OS << Separator;
141 if (SectionAttrDescriptors[i].AssemblerName)
142 OS << SectionAttrDescriptors[i].AssemblerName;
143 else
144 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
145 Separator = '+';
146 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000147
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000148 assert(SectionAttrs == 0 && "Unknown section attributes!");
Jim Grosbachfe40f952010-10-21 22:04:05 +0000149
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000150 // If we have a S_SYMBOL_STUBS size specified, print it.
151 if (Reserved2 != 0)
152 OS << ',' << Reserved2;
153 OS << '\n';
154}
155
Jan Wen Voung083cf152010-10-04 17:32:41 +0000156bool MCSectionMachO::UseCodeAlign() const {
157 return hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
158}
159
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000160bool MCSectionMachO::isVirtualSection() const {
161 return (getType() == MCSectionMachO::S_ZEROFILL ||
162 getType() == MCSectionMachO::S_GB_ZEROFILL ||
163 getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
164}
165
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000166/// StripSpaces - This removes leading and trailing spaces from the StringRef.
167static void StripSpaces(StringRef &Str) {
168 while (!Str.empty() && isspace(Str[0]))
169 Str = Str.substr(1);
170 while (!Str.empty() && isspace(Str.back()))
171 Str = Str.substr(0, Str.size()-1);
172}
173
174/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
175/// This is a string that can appear after a .section directive in a mach-o
176/// flavored .s file. If successful, this fills in the specified Out
177/// parameters and returns an empty string. When an invalid section
178/// specifier is present, this returns a string indicating the problem.
179std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
180 StringRef &Segment, // Out.
181 StringRef &Section, // Out.
182 unsigned &TAA, // Out.
183 unsigned &StubSize) { // Out.
184 // Find the first comma.
185 std::pair<StringRef, StringRef> Comma = Spec.split(',');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000186
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000187 // If there is no comma, we fail.
188 if (Comma.second.empty())
189 return "mach-o section specifier requires a segment and section "
190 "separated by a comma";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000191
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000192 // Capture segment, remove leading and trailing whitespace.
193 Segment = Comma.first;
194 StripSpaces(Segment);
195
196 // Verify that the segment is present and not too long.
197 if (Segment.empty() || Segment.size() > 16)
198 return "mach-o section specifier requires a segment whose length is "
199 "between 1 and 16 characters";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000200
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000201 // Split the section name off from any attributes if present.
202 Comma = Comma.second.split(',');
203
204 // Capture section, remove leading and trailing whitespace.
205 Section = Comma.first;
206 StripSpaces(Section);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000207
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000208 // Verify that the section is present and not too long.
209 if (Section.empty() || Section.size() > 16)
210 return "mach-o section specifier requires a section whose length is "
211 "between 1 and 16 characters";
212
213 // If there is no comma after the section, we're done.
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000214 StubSize = 0;
215 if (Comma.second.empty())
216 return "";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000217
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000218 // Otherwise, we need to parse the section type and attributes.
219 Comma = Comma.second.split(',');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000220
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000221 // Get the section type.
222 StringRef SectionType = Comma.first;
223 StripSpaces(SectionType);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000224
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000225 // Figure out which section type it is.
226 unsigned TypeID;
227 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
228 if (SectionTypeDescriptors[TypeID].AssemblerName &&
229 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
230 break;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000231
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000232 // If we didn't find the section type, reject it.
233 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
234 return "mach-o section specifier uses an unknown section type";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000235
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000236 // Remember the TypeID.
237 TAA = TypeID;
238
239 // If we have no comma after the section type, there are no attributes.
240 if (Comma.second.empty()) {
241 // S_SYMBOL_STUBS always require a symbol stub size specifier.
242 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
243 return "mach-o section specifier of type 'symbol_stubs' requires a size "
244 "specifier";
245 return "";
246 }
247
248 // Otherwise, we do have some attributes. Split off the size specifier if
249 // present.
250 Comma = Comma.second.split(',');
251 StringRef Attrs = Comma.first;
Jim Grosbachfe40f952010-10-21 22:04:05 +0000252
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000253 // The attribute list is a '+' separated list of attributes.
254 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
Jim Grosbachfe40f952010-10-21 22:04:05 +0000255
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000256 while (1) {
257 StringRef Attr = Plus.first;
258 StripSpaces(Attr);
259
260 // Look up the attribute.
261 for (unsigned i = 0; ; ++i) {
Kevin Enderby5440f632009-10-07 20:57:20 +0000262 if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000263 return "mach-o section specifier has invalid attribute";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000264
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000265 if (SectionAttrDescriptors[i].AssemblerName &&
266 Attr == SectionAttrDescriptors[i].AssemblerName) {
267 TAA |= SectionAttrDescriptors[i].AttrFlag;
268 break;
269 }
270 }
Jim Grosbachfe40f952010-10-21 22:04:05 +0000271
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000272 if (Plus.second.empty()) break;
273 Plus = Plus.second.split('+');
274 };
275
276 // Okay, we've parsed the section attributes, see if we have a stub size spec.
277 if (Comma.second.empty()) {
278 // S_SYMBOL_STUBS always require a symbol stub size specifier.
279 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
280 return "mach-o section specifier of type 'symbol_stubs' requires a size "
281 "specifier";
282 return "";
283 }
284
285 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
286 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
287 return "mach-o section specifier cannot have a stub size specified because "
288 "it does not have type 'symbol_stubs'";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000289
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000290 // Okay, if we do, it must be a number.
291 StringRef StubSizeStr = Comma.second;
292 StripSpaces(StubSizeStr);
Jim Grosbachfe40f952010-10-21 22:04:05 +0000293
Chris Lattnerd9221d72009-09-20 06:58:54 +0000294 // Convert the stub size from a string to an integer.
295 if (StubSizeStr.getAsInteger(0, StubSize))
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000296 return "mach-o section specifier has a malformed stub size";
Jim Grosbachfe40f952010-10-21 22:04:05 +0000297
Chris Lattnerf9bdedd2009-08-10 18:15:01 +0000298 return "";
299}