llvm.org GIT mirror llvm / ff4bc46
Make the big switch: Change MCSectionMachO to represent a section *semantically* instead of syntactically as a string. This means that it keeps track of the segment, section, flags, etc directly and asmprints them in the right format. This also includes parsing and validation support for llvm-mc and "attribute(section)", so we should now start getting errors about invalid section attributes from the compiler instead of the assembler on darwin. Still todo: 1) Uniquing of darwin mcsections 2) Move all the Darwin stuff out to MCSectionMachO.[cpp|h] 3) there are a few FIXMEs, for example what is the syntax to get the S_GB_ZEROFILL segment type? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78547 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
18 changed file(s) with 750 addition(s) and 308 deletion(s). Raw diff Collapse all Expand all
6666 raw_ostream &OS) const;
6767 };
6868
69
70 /// MCSectionMachO - This represents a section on a Mach-O system (used by
71 /// Mac OS X). On a Mac system, these are also described in
72 /// /usr/include/mach-o/loader.h.
6973 class MCSectionMachO : public MCSection {
74 char SegmentName[16]; // Not necessarily null terminated!
75 char SectionName[16]; // Not necessarily null terminated!
76
77 /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
78 /// field of a section, drawn from the enums below.
79 unsigned TypeAndAttributes;
80
81 /// Reserved2 - The 'reserved2' field of a section, used to represent the
82 /// size of stubs, for example.
83 unsigned Reserved2;
84
85 MCSectionMachO(const StringRef &Segment, const StringRef &Section,
86 unsigned TAA, unsigned reserved2, SectionKind K)
87 : MCSection(K), TypeAndAttributes(TAA), 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 public:
103
104 static MCSectionMachO *Create(const StringRef &Segment,
105 const StringRef &Section,
106 unsigned TypeAndAttributes,
107 unsigned Reserved2,
108 SectionKind K, MCContext &Ctx);
109
110 /// These are the section type and attributes fields. A MachO section can
111 /// have only one Type, but can have any of the attributes specified.
112 enum {
113 // TypeAndAttributes bitmasks.
114 SECTION_TYPE = 0x000000FFU,
115 SECTION_ATTRIBUTES = 0xFFFFFF00U,
116
117 // Valid section types.
118
119 /// S_REGULAR - Regular section.
120 S_REGULAR = 0x00U,
121 /// S_ZEROFILL - Zero fill on demand section.
122 S_ZEROFILL = 0x01U,
123 /// S_CSTRING_LITERALS - Section with literal C strings.
124 S_CSTRING_LITERALS = 0x02U,
125 /// S_4BYTE_LITERALS - Section with 4 byte literals.
126 S_4BYTE_LITERALS = 0x03U,
127 /// S_8BYTE_LITERALS - Section with 8 byte literals.
128 S_8BYTE_LITERALS = 0x04U,
129 /// S_LITERAL_POINTERS - Section with pointers to literals.
130 S_LITERAL_POINTERS = 0x05U,
131 /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
132 S_NON_LAZY_SYMBOL_POINTERS = 0x06U,
133 /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
134 S_LAZY_SYMBOL_POINTERS = 0x07U,
135 /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
136 /// the Reserved2 field.
137 S_SYMBOL_STUBS = 0x08U,
138 /// S_SYMBOL_STUBS - Section with only function pointers for
139 /// initialization.
140 S_MOD_INIT_FUNC_POINTERS = 0x09U,
141 /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
142 /// termination.
143 S_MOD_TERM_FUNC_POINTERS = 0x0AU,
144 /// S_COALESCED - Section contains symbols that are to be coalesced.
145 S_COALESCED = 0x0BU,
146 /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
147 /// gigabytes).
148 S_GB_ZEROFILL = 0x0CU,
149 /// S_INTERPOSING - Section with only pairs of function pointers for
150 /// interposing.
151 S_INTERPOSING = 0x0DU,
152 /// S_16BYTE_LITERALS - Section with only 16 byte literals.
153 S_16BYTE_LITERALS = 0x0EU,
154 /// S_DTRACE_DOF - Section contains DTrace Object Format.
155 S_DTRACE_DOF = 0x0FU,
156 /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
157 /// lazy loaded dylibs.
158 S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
159
160 LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS,
161
162
163 // Valid section attributes.
164
165 /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
166 /// instructions.
167 S_ATTR_PURE_INSTRUCTIONS = 1U << 31,
168 /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
169 /// in a ranlib table of contents.
170 S_ATTR_NO_TOC = 1U << 30,
171 /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
172 /// in files with the MY_DYLDLINK flag.
173 S_ATTR_STRIP_STATIC_SYMS = 1U << 29,
174 /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
175 S_ATTR_NO_DEAD_STRIP = 1U << 28,
176 /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
177 S_ATTR_LIVE_SUPPORT = 1U << 27,
178 /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
179 /// dyld.
180 S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
181 /// S_ATTR_DEBUG - A debug section.
182 S_ATTR_DEBUG = 1U << 25,
183 /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
184 S_ATTR_SOME_INSTRUCTIONS = 1U << 10,
185 /// S_ATTR_EXT_RELOC - Section has external relocation entries.
186 S_ATTR_EXT_RELOC = 1U << 9,
187 /// S_ATTR_LOC_RELOC - Section has local relocation entries.
188 S_ATTR_LOC_RELOC = 1U << 8
189 };
190
191 StringRef getSegmentName() const {
192 // SegmentName is not necessarily null terminated!
193 if (SegmentName[15])
194 return StringRef(SegmentName, 16);
195 return StringRef(SegmentName);
196 }
197 StringRef getSectionName() const {
198 // SectionName is not necessarily null terminated!
199 if (SectionName[15])
200 return StringRef(SectionName, 16);
201 return StringRef(SectionName);
202 }
203
204 unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
205
206
207 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
208 /// This is a string that can appear after a .section directive in a mach-o
209 /// flavored .s file. If successful, this fills in the specified Out
210 /// parameters and returns an empty string. When an invalid section
211 /// specifier is present, this returns a string indicating the problem.
212 static std::string ParseSectionSpecifier(StringRef Spec, // In.
213 StringRef &Segment, // Out.
214 StringRef &Section, // Out.
215 unsigned &TAA, // Out.
216 unsigned &StubSize); // Out.
217
218 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
219 raw_ostream &OS) const;
220 };
221
222 class MCSectionCOFF : public MCSection {
70223 std::string Name;
71224
72225 /// IsDirective - This is true if the section name is a directive, not
76229 /// of a syntactic one.
77230 bool IsDirective;
78231
79 MCSectionMachO(const StringRef &Name, bool IsDirective, SectionKind K,
80 MCContext &Ctx);
81 public:
82
83 static MCSectionMachO *Create(const StringRef &Name, bool IsDirective,
84 SectionKind K, MCContext &Ctx);
232 MCSectionCOFF(const StringRef &Name, bool IsDirective, SectionKind K,
233 MCContext &Ctx);
234 public:
235
236 static MCSectionCOFF *Create(const StringRef &Name, bool IsDirective,
237 SectionKind K, MCContext &Ctx);
85238
86239 const std::string &getName() const { return Name; }
87240 bool isDirective() const { return IsDirective; }
90243 raw_ostream &OS) const;
91244 };
92245
93 class MCSectionCOFF : public MCSection {
94 std::string Name;
95
96 /// IsDirective - This is true if the section name is a directive, not
97 /// something that should be printed with ".section".
98 ///
99 /// FIXME: This is a hack. Switch to a semantic view of the section instead
100 /// of a syntactic one.
101 bool IsDirective;
102
103 MCSectionCOFF(const StringRef &Name, bool IsDirective, SectionKind K,
104 MCContext &Ctx);
105 public:
106
107 static MCSectionCOFF *Create(const StringRef &Name, bool IsDirective,
108 SectionKind K, MCContext &Ctx);
109
110 const std::string &getName() const { return Name; }
111 bool isDirective() const { return IsDirective; }
112
113 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
114 raw_ostream &OS) const;
115 };
116
117246 } // end namespace llvm
118247
119248 #endif
256256 Mangler *) const;
257257
258258 /// getMachOSection - Return the MCSection for the specified mach-o section.
259 /// FIXME: Switch this to a semantic view eventually.
260 const MCSection *getMachOSection(const char *Name, bool isDirective,
261 SectionKind K) const;
259 /// This requires the operands to be valid.
260 const MCSection *getMachOSection(StringRef Segment, StringRef Section,
261 unsigned TypeAndAttributes,
262 SectionKind K) const {
263 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
264 }
265 const MCSection *getMachOSection(StringRef Segment, StringRef Section,
266 unsigned TypeAndAttributes,
267 unsigned Reserved2, SectionKind K) const;
268
269 /// getTextCoalSection - Return the "__TEXT,__textcoal_nt" section we put weak
270 /// symbols into.
271 const MCSection *getTextCoalSection() const {
272 return TextCoalSection;
273 }
274
275 /// getLazySymbolPointerSection - Return the section corresponding to
276 /// the .lazy_symbol_pointer directive.
277 const MCSection *getLazySymbolPointerSection() const;
278
279 /// getNonLazySymbolPointerSection - Return the section corresponding to
280 /// the .non_lazy_symbol_pointer directive.
281 const MCSection *getNonLazySymbolPointerSection() const;
282
262283 };
263284
264285
221221 OS << ".zerofill ";
222222
223223 // This is a mach-o specific directive.
224 OS << '"' << ((MCSectionMachO*)Section)->getName() << '"';
224 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
225 OS << '"' << MOSection->getSegmentName() << ","
226 << MOSection->getSectionName() << '"';
225227
226228
227229 if (Symbol != NULL) {
114114 // MCSectionMachO
115115 //===----------------------------------------------------------------------===//
116116
117 /// SectionTypeDescriptors - These are strings that describe the various section
118 /// types. This *must* be kept in order with and stay synchronized with the
119 /// section type list.
120 static const struct {
121 const char *AssemblerName, *EnumName;
122 } SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
123 { "regular", "S_REGULAR" }, // 0x00
124 { 0, "S_ZEROFILL" }, // 0x01
125 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
126 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
127 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
128 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
129 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
130 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
131 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
132 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
133 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
134 { "coalesced", "S_COALESCED" }, // 0x0B
135 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
136 { "interposing", "S_INTERPOSING" }, // 0x0D
137 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
138 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
139 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10
140 };
141
142
143 /// SectionAttrDescriptors - This is an array of descriptors for section
144 /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
145 /// by attribute, instead it is searched. The last entry has a zero AttrFlag
146 /// value.
147 static const struct {
148 unsigned AttrFlag;
149 const char *AssemblerName, *EnumName;
150 } SectionAttrDescriptors[] = {
151 #define ENTRY(ASMNAME, ENUM) \
152 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
153 ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
154 ENTRY("no_toc", S_ATTR_NO_TOC)
155 ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
156 ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
157 ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
158 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
159 ENTRY("debug", S_ATTR_DEBUG)
160 ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
161 ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
162 ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
163 #undef ENTRY
164 { 0, "none", 0 }
165 };
166
167
117168 MCSectionMachO *MCSectionMachO::
118 Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
119 return new (Ctx) MCSectionMachO(Name, IsDirective, K, Ctx);
120 }
121
122 MCSectionMachO::MCSectionMachO(const StringRef &name, bool isDirective,
123 SectionKind K, MCContext &Ctx)
124 : MCSection(K), Name(name), IsDirective(isDirective) {
125 Ctx.SetSection(Name, this);
169 Create(const StringRef &Segment, const StringRef &Section,
170 unsigned TypeAndAttributes, unsigned Reserved2,
171 SectionKind K, MCContext &Ctx) {
172 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero.
173 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
174 Reserved2, K);
126175 }
127176
128177 void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
129178 raw_ostream &OS) const {
130 if (!isDirective())
131 OS << "\t.section\t" << getName() << '\n';
179 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
180
181 // Get the section type and attributes.
182 unsigned TAA = getTypeAndAttributes();
183 if (TAA == 0) {
184 OS << '\n';
185 return;
186 }
187
188 OS << ',';
189
190 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
191 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
192 "Invalid SectionType specified!");
193
194 if (SectionTypeDescriptors[SectionType].AssemblerName)
195 OS << SectionTypeDescriptors[SectionType].AssemblerName;
132196 else
133 OS << getName() << '\n';
134 }
197 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
198
199 // If we don't have any attributes, we're done.
200 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
201 if (SectionAttrs == 0) {
202 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
203 // the attribute specifier.
204 if (Reserved2 != 0)
205 OS << ",none," << Reserved2;
206 OS << '\n';
207 return;
208 }
209
210 // Check each attribute to see if we have it.
211 char Separator = ',';
212 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
213 // Check to see if we have this attribute.
214 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
215 continue;
216
217 // Yep, clear it and print it.
218 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
219
220 OS << Separator;
221 if (SectionAttrDescriptors[i].AssemblerName)
222 OS << SectionAttrDescriptors[i].AssemblerName;
223 else
224 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
225 Separator = '+';
226 }
227
228 assert(SectionAttrs == 0 && "Unknown section attributes!");
229
230 // If we have a S_SYMBOL_STUBS size specified, print it.
231 if (Reserved2 != 0)
232 OS << ',' << Reserved2;
233 OS << '\n';
234 }
235
236 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
237 static void StripSpaces(StringRef &Str) {
238 while (!Str.empty() && isspace(Str[0]))
239 Str = Str.substr(1);
240 while (!Str.empty() && isspace(Str.back()))
241 Str = Str.substr(0, Str.size()-1);
242 }
243
244 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
245 /// This is a string that can appear after a .section directive in a mach-o
246 /// flavored .s file. If successful, this fills in the specified Out
247 /// parameters and returns an empty string. When an invalid section
248 /// specifier is present, this returns a string indicating the problem.
249 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
250 StringRef &Segment, // Out.
251 StringRef &Section, // Out.
252 unsigned &TAA, // Out.
253 unsigned &StubSize) { // Out.
254 // Find the first comma.
255 std::pair Comma = Spec.split(',');
256
257 // If there is no comma, we fail.
258 if (Comma.second.empty())
259 return "mach-o section specifier requires a segment and section "
260 "separated by a comma";
261
262 // Capture segment, remove leading and trailing whitespace.
263 Segment = Comma.first;
264 StripSpaces(Segment);
265
266 // Verify that the segment is present and not too long.
267 if (Segment.empty() || Segment.size() > 16)
268 return "mach-o section specifier requires a segment whose length is "
269 "between 1 and 16 characters";
270
271 // Split the section name off from any attributes if present.
272 Comma = Comma.second.split(',');
273
274 // Capture section, remove leading and trailing whitespace.
275 Section = Comma.first;
276 StripSpaces(Section);
277
278 // Verify that the section is present and not too long.
279 if (Section.empty() || Section.size() > 16)
280 return "mach-o section specifier requires a section whose length is "
281 "between 1 and 16 characters";
282
283 // If there is no comma after the section, we're done.
284 TAA = 0;
285 StubSize = 0;
286 if (Comma.second.empty())
287 return "";
288
289 // Otherwise, we need to parse the section type and attributes.
290 Comma = Comma.second.split(',');
291
292 // Get the section type.
293 StringRef SectionType = Comma.first;
294 StripSpaces(SectionType);
295
296 // Figure out which section type it is.
297 unsigned TypeID;
298 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
299 if (SectionTypeDescriptors[TypeID].AssemblerName &&
300 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
301 break;
302
303 // If we didn't find the section type, reject it.
304 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
305 return "mach-o section specifier uses an unknown section type";
306
307 // Remember the TypeID.
308 TAA = TypeID;
309
310 // If we have no comma after the section type, there are no attributes.
311 if (Comma.second.empty()) {
312 // S_SYMBOL_STUBS always require a symbol stub size specifier.
313 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
314 return "mach-o section specifier of type 'symbol_stubs' requires a size "
315 "specifier";
316 return "";
317 }
318
319 // Otherwise, we do have some attributes. Split off the size specifier if
320 // present.
321 Comma = Comma.second.split(',');
322 StringRef Attrs = Comma.first;
323
324 // The attribute list is a '+' separated list of attributes.
325 std::pair Plus = Attrs.split('+');
326
327 while (1) {
328 StringRef Attr = Plus.first;
329 StripSpaces(Attr);
330
331 // Look up the attribute.
332 for (unsigned i = 0; ; ++i) {
333 if (SectionAttrDescriptors[i].AttrFlag == 0)
334 return "mach-o section specifier has invalid attribute";
335
336 if (SectionAttrDescriptors[i].AssemblerName &&
337 Attr == SectionAttrDescriptors[i].AssemblerName) {
338 TAA |= SectionAttrDescriptors[i].AttrFlag;
339 break;
340 }
341 }
342
343 if (Plus.second.empty()) break;
344 Plus = Plus.second.split('+');
345 };
346
347 // Okay, we've parsed the section attributes, see if we have a stub size spec.
348 if (Comma.second.empty()) {
349 // S_SYMBOL_STUBS always require a symbol stub size specifier.
350 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
351 return "mach-o section specifier of type 'symbol_stubs' requires a size "
352 "specifier";
353 return "";
354 }
355
356 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
357 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
358 return "mach-o section specifier cannot have a stub size specified because "
359 "it does not have type 'symbol_stubs'";
360
361 // Okay, if we do, it must be a number.
362 StringRef StubSizeStr = Comma.second;
363 StripSpaces(StubSizeStr);
364
365 // Convert the a null terminated buffer for strtoul.
366 char TmpBuffer[32];
367 if (StubSizeStr.size() >= 32)
368 return"mach-o section specifier has a stub size specifier that is too long";
369
370 memcpy(TmpBuffer, StubSizeStr.data(), StubSizeStr.size());
371 TmpBuffer[StubSizeStr.size()] = 0;
372
373 char *EndPtr;
374 StubSize = strtoul(TmpBuffer, &EndPtr, 0);
375
376 if (EndPtr[0] != 0)
377 return "mach-o section specifier has a malformed stub size";
378
379 return "";
380 }
381
135382
136383 //===----------------------------------------------------------------------===//
137384 // MCSectionCOFF
12851285 O << '\n';
12861286
12871287 if (!FnStubs.empty()) {
1288 const MCSection *StubSection;
1289 if (TM.getRelocationModel() == Reloc::PIC_)
1290 StubSection = TLOFMacho.getMachOSection(".section __TEXT,__picsymbolstu"
1291 "b4,symbol_stubs,none,16", true,
1292 SectionKind::getText());
1293 else
1294 StubSection = TLOFMacho.getMachOSection(".section __TEXT,__symbol_stub4"
1295 ",symbol_stubs,none,12", true,
1296 SectionKind::getText());
1288 unsigned StubSize = 12;
1289 const char *StubSectionName = "__symbol_stub4";
1290
1291 if (TM.getRelocationModel() == Reloc::PIC_) {
1292 StubSize = 16;
1293 StubSectionName = "__picsymbolstub4";
1294 }
1295
1296 const MCSection *StubSection
1297 = TLOFMacho.getMachOSection("__TEXT", StubSectionName,
1298 MCSectionMachO::S_SYMBOL_STUBS,
1299 StubSize, SectionKind::getText());
12971300
12981301 const MCSection *LazySymbolPointerSection
1299 = TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1300 SectionKind::getMetadata());
1302 = TLOFMacho.getLazySymbolPointerSection();
13011303
13021304 // Output stubs for dynamically-linked functions
13031305 for (StringMap::iterator I = FnStubs.begin(),
13321334
13331335 // Output non-lazy-pointers for external and common global variables.
13341336 if (!GVNonLazyPtrs.empty()) {
1335 SwitchToSection(TLOFMacho.getMachOSection(".non_lazy_symbol_pointer",
1336 true,
1337 SectionKind::getMetadata()));
1337 // Switch with ".non_lazy_symbol_pointer" directive.
1338 SwitchToSection(TLOFMacho.getNonLazySymbolPointerSection());
13381339 for (StringMap::iterator I = GVNonLazyPtrs.begin(),
13391340 E = GVNonLazyPtrs.end(); I != E; ++I) {
13401341 O << I->second << ":\n";
845845
846846 // Prime text sections so they are adjacent. This reduces the likelihood a
847847 // large data or debug section causes a branch to exceed 16M limit.
848
849848 TargetLoweringObjectFileMachO &TLOFMacho =
850849 static_cast(getObjFileLowering());
851 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__textcoal_nt,"
852 "coalesced,pure_instructions", true,
853 SectionKind::getText()));
850 SwitchToSection(TLOFMacho.getTextCoalSection());
854851 if (TM.getRelocationModel() == Reloc::PIC_) {
855 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstu"
856 "b1,symbol_stubs,"
857 "pure_instructions,32", true,
858 SectionKind::getText()));
852 SwitchToSection(TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
853 MCSectionMachO::S_SYMBOL_STUBS |
854 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
855 32, SectionKind::getText()));
859856 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
860 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1"
861 ",symbol_stubs,"
862 "pure_instructions,16", true,
863 SectionKind::getText()));
857 SwitchToSection(TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
858 MCSectionMachO::S_SYMBOL_STUBS |
859 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
860 16, SectionKind::getText()));
864861 }
865862 SwitchToSection(getObjFileLowering().getTextSection());
866863
984981 TargetLoweringObjectFileMachO &TLOFMacho =
985982 static_cast(getObjFileLowering());
986983
984
985 const MCSection *LSPSection = 0;
986 if (!FnStubs.empty()) // .lazy_symbol_pointer
987 LSPSection = TLOFMacho.getLazySymbolPointerSection();
988
989
987990 // Output stubs for dynamically-linked functions
988991 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
989992 const MCSection *StubSection =
990 TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstub1,"
991 "symbol_stubs,pure_instructions,32", true,
992 SectionKind::getText());
993 const MCSection *LSPSection =
994 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
995 SectionKind::getMetadata());
996
997 for (StringMap::iterator I = FnStubs.begin(), E = FnStubs.end();
993 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
994 MCSectionMachO::S_SYMBOL_STUBS |
995 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
996 32, SectionKind::getText());
997 for (StringMap::iterator I = FnStubs.begin(), E = FnStubs.end();
998998 I != E; ++I) {
999999 SwitchToSection(StubSection);
10001000 EmitAlignment(4);
10191019 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
10201020 }
10211021 } else if (!FnStubs.empty()) {
1022 const MCSection *StubSection =
1023 TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1024 "pure_instructions,16", true,
1025 SectionKind::getText());
1026 const MCSection *LSPSection =
1027 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1028 SectionKind::getMetadata());
1022 const MCSection *StubSection =
1023 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1024 MCSectionMachO::S_SYMBOL_STUBS |
1025 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1026 16, SectionKind::getText());
10291027
10301028 for (StringMap::iterator I = FnStubs.begin(), E = FnStubs.end();
10311029 I != E; ++I) {
10621060
10631061 // Output macho stubs for external and common global variables.
10641062 if (!GVStubs.empty()) {
1065 const MCSection *TheSection =
1066 TLOFMacho.getMachOSection(".non_lazy_symbol_pointer", true,
1067 SectionKind::getMetadata());
1068 SwitchToSection(TheSection);
1063 // Switch with ".non_lazy_symbol_pointer" directive.
1064 SwitchToSection(TLOFMacho.getNonLazySymbolPointerSection());
10691065 for (StringMap::iterator I = GVStubs.begin(),
10701066 E = GVStubs.end(); I != E; ++I) {
10711067 O << I->second << ":\n";
509509
510510
511511 const MCSection *TargetLoweringObjectFileMachO::
512 getMachOSection(const char *Name, bool isDirective, SectionKind Kind) const {
513 if (MCSection *S = getContext().GetSection(Name))
514 return S;
515 return MCSectionMachO::Create(Name, isDirective, Kind, getContext());
516 }
517
512 getMachOSection(StringRef Segment, StringRef Section,
513 unsigned TypeAndAttributes,
514 unsigned Reserved2, SectionKind Kind) const {
515 // FIXME: UNIQUE HERE.
516 //if (MCSection *S = getContext().GetSection(Name))
517 // return S;
518
519 return MCSectionMachO::Create(Segment, Section, TypeAndAttributes, Reserved2,
520 Kind, getContext());
521 }
518522
519523
520524 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
521525 const TargetMachine &TM) {
522526 TargetLoweringObjectFile::Initialize(Ctx, TM);
523 TextSection = getMachOSection("\t.text", true, SectionKind::getText());
524 DataSection = getMachOSection("\t.data", true, SectionKind::getDataRel());
525
526 CStringSection = getMachOSection("\t.cstring", true,
527 SectionKind::getMergeable1ByteCString());
528 UStringSection = getMachOSection("__TEXT,__ustring", false,
529 SectionKind::getMergeable2ByteCString());
530 FourByteConstantSection = getMachOSection("\t.literal4\n", true,
531 SectionKind::getMergeableConst4());
532 EightByteConstantSection = getMachOSection("\t.literal8\n", true,
533 SectionKind::getMergeableConst8());
527
528 TextSection // .text
529 = getMachOSection("__TEXT", "__text",
530 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
531 SectionKind::getText());
532 DataSection // .data
533 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
534
535 CStringSection // .cstring
536 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
537 SectionKind::getMergeable1ByteCString());
538 UStringSection
539 = getMachOSection("__TEXT","__ustring", 0,
540 SectionKind::getMergeable2ByteCString());
541 FourByteConstantSection // .literal4
542 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
543 SectionKind::getMergeableConst4());
544 EightByteConstantSection // .literal8
545 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
546 SectionKind::getMergeableConst8());
534547
535548 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
536549 // to using it in -static mode.
550 SixteenByteConstantSection = 0;
537551 if (TM.getRelocationModel() != Reloc::Static &&
538552 TM.getTargetData()->getPointerSize() == 32)
539 SixteenByteConstantSection =
540 getMachOSection("\t.literal16\n", true,
553 SixteenByteConstantSection = // .literal16
554 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
541555 SectionKind::getMergeableConst16());
542 else
543 SixteenByteConstantSection = 0;
544
545 ReadOnlySection = getMachOSection("\t.const", true,
546 SectionKind::getReadOnly());
547
548 TextCoalSection =
549 getMachOSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
550 false, SectionKind::getText());
551 ConstTextCoalSection = getMachOSection("\t__TEXT,__const_coal,coalesced",
552 false, SectionKind::getText());
553 ConstDataCoalSection = getMachOSection("\t__DATA,__const_coal,coalesced",
554 false, SectionKind::getText());
555 ConstDataSection = getMachOSection("\t.const_data", true,
556 SectionKind::getReadOnlyWithRel());
557 DataCoalSection = getMachOSection("\t__DATA,__datacoal_nt,coalesced",
558 false, SectionKind::getDataRel());
556
557 ReadOnlySection // .const
558 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
559
560 TextCoalSection
561 = getMachOSection("__TEXT", "__textcoal_nt",
562 MCSectionMachO::S_COALESCED |
563 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
564 SectionKind::getText());
565 ConstTextCoalSection
566 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
567 SectionKind::getText());
568 ConstDataCoalSection
569 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
570 SectionKind::getText());
571 ConstDataSection // .const_data
572 = getMachOSection("__DATA", "__const", 0,
573 SectionKind::getReadOnlyWithRel());
574 DataCoalSection
575 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
576 SectionKind::getDataRel());
559577
560578 if (TM.getRelocationModel() == Reloc::Static) {
561 StaticCtorSection =
562 getMachOSection(".constructor", true, SectionKind::getDataRel());
563 StaticDtorSection =
564 getMachOSection(".destructor", true, SectionKind::getDataRel());
579 StaticCtorSection
580 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
581 StaticDtorSection
582 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
565583 } else {
566 StaticCtorSection =
567 getMachOSection(".mod_init_func", true, SectionKind::getDataRel());
568 StaticDtorSection =
569 getMachOSection(".mod_term_func", true, SectionKind::getDataRel());
584 StaticCtorSection
585 = getMachOSection("__DATA", "__mod_init_func",
586 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
587 SectionKind::getDataRel());
588 StaticDtorSection
589 = getMachOSection("__DATA", "__mod_term_func",
590 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
591 SectionKind::getDataRel());
570592 }
571593
572594 // Exception Handling.
573 LSDASection = getMachOSection("__DATA,__gcc_except_tab", false,
595 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
574596 SectionKind::getDataRel());
575597 EHFrameSection =
576 getMachOSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
577 "+live_support", false, SectionKind::getReadOnly());
598 getMachOSection("__TEXT", "__eh_frame",
599 MCSectionMachO::S_COALESCED |
600 MCSectionMachO::S_ATTR_NO_TOC |
601 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
602 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
603 SectionKind::getReadOnly());
578604
579605 // Debug Information.
580 // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
581 // FIXME: Need __DWARF segment.
582606 DwarfAbbrevSection =
583 getMachOSection(".section __DWARF,__debug_abbrev,regular,debug", true,
607 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
584608 SectionKind::getMetadata());
585609 DwarfInfoSection =
586 getMachOSection(".section __DWARF,__debug_info,regular,debug", true,
610 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
587611 SectionKind::getMetadata());
588612 DwarfLineSection =
589 getMachOSection(".section __DWARF,__debug_line,regular,debug", true,
613 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
590614 SectionKind::getMetadata());
591615 DwarfFrameSection =
592 getMachOSection(".section __DWARF,__debug_frame,regular,debug", true,
616 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
593617 SectionKind::getMetadata());
594618 DwarfPubNamesSection =
595 getMachOSection(".section __DWARF,__debug_pubnames,regular,debug", true,
619 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
596620 SectionKind::getMetadata());
597621 DwarfPubTypesSection =
598 getMachOSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
622 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
599623 SectionKind::getMetadata());
600624 DwarfStrSection =
601 getMachOSection(".section __DWARF,__debug_str,regular,debug", true,
625 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
602626 SectionKind::getMetadata());
603627 DwarfLocSection =
604 getMachOSection(".section __DWARF,__debug_loc,regular,debug", true,
628 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
605629 SectionKind::getMetadata());
606630 DwarfARangesSection =
607 getMachOSection(".section __DWARF,__debug_aranges,regular,debug", true,
631 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
608632 SectionKind::getMetadata());
609633 DwarfRangesSection =
610 getMachOSection(".section __DWARF,__debug_ranges,regular,debug", true,
634 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
611635 SectionKind::getMetadata());
612636 DwarfMacroInfoSection =
613 getMachOSection(".section __DWARF,__debug_macinfo,regular,debug", true,
637 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
614638 SectionKind::getMetadata());
615639 DwarfDebugInlineSection =
616 getMachOSection(".section __DWARF,__debug_inlined,regular,debug", true,
617 SectionKind::getMetadata());
618 }
640 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
641 SectionKind::getMetadata());
642 }
643
644 /// getLazySymbolPointerSection - Return the section corresponding to
645 /// the .lazy_symbol_pointer directive.
646 const MCSection *TargetLoweringObjectFileMachO::
647 getLazySymbolPointerSection() const {
648 return getMachOSection("__DATA", "__la_symbol_ptr",
649 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
650 SectionKind::getMetadata());
651 }
652
653 /// getNonLazySymbolPointerSection - Return the section corresponding to
654 /// the .non_lazy_symbol_pointer directive.
655 const MCSection *TargetLoweringObjectFileMachO::
656 getNonLazySymbolPointerSection() const {
657 return getMachOSection("__DATA", "__nl_symbol_ptr",
658 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
659 SectionKind::getMetadata());
660 }
661
619662
620663 const MCSection *TargetLoweringObjectFileMachO::
621664 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
622665 Mangler *Mang, const TargetMachine &TM) const {
623 return getMachOSection(GV->getSection().c_str(), false, Kind);
666 // Parse the section specifier and create it if valid.
667 StringRef Segment, Section;
668 unsigned TAA, StubSize;
669 std::string ErrorCode =
670 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
671 TAA, StubSize);
672 if (ErrorCode.empty())
673 return getMachOSection(Segment, Section, TAA, StubSize, Kind);
674
675
676 // If invalid, report the error with llvm_report_error.
677 llvm_report_error("Global variable '" + GV->getNameStr() +
678 "' has an invalid section specifier '" + GV->getSection() +
679 "': " + ErrorCode + ".");
680 // Fall back to dropping it into the data section.
681 return DataSection;
624682 }
625683
626684 const MCSection *TargetLoweringObjectFileMachO::
917917 // Output stubs for dynamically-linked functions
918918 if (!FnStubs.empty()) {
919919 const MCSection *TheSection =
920 TLOFMacho.getMachOSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
921 "self_modifying_code+pure_instructions,5", true,
922 SectionKind::getMetadata());
920 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
921 MCSectionMachO::S_SYMBOL_STUBS |
922 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
923 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
924 5, SectionKind::getMetadata());
923925 SwitchToSection(TheSection);
924926 for (StringMap::iterator I = FnStubs.begin(),
925927 E = FnStubs.end(); I != E; ++I)
931933 // Output stubs for external and common global variables.
932934 if (!GVStubs.empty()) {
933935 const MCSection *TheSection =
934 TLOFMacho.getMachOSection("\t.section __IMPORT,__pointers,"
935 "non_lazy_symbol_pointers", true,
936 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
937 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
936938 SectionKind::getMetadata());
937939 SwitchToSection(TheSection);
938940 for (StringMap::iterator I = GVStubs.begin(),
None ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin | FileCheck %s -check-prefix=DARWIN
1 ; RUN: llvm-as < %s | llc -mtriple=arm-linux-gnu | FileCheck %s -check-prefix=ELF
0 ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin | FileCheck %s -check-prefix=DARWIN
1 ; RUN: llvm-as < %s | llc -mtriple=arm-linux-gnu | FileCheck %s -check-prefix=ELF
22 ; RUN: llvm-as < %s | llc -mtriple=arm-linux-gnueabi | FileCheck %s -check-prefix=GNUEABI
33
4 ; DARWIN: .mod_init_func
5 ; DARWIN: .mod_term_func
4 ; DARWIN: .section __DATA,__mod_init_func,mod_init_funcs
5 ; DARWIN: .section __DATA,__mod_term_func,mod_term_funcs
66
77 ; ELF: .section .ctors,"aw",%progbits
88 ; ELF: .section .dtors,"aw",%progbits
3838 ; PIC: L_exact_log2$slp:
3939 ; PIC: .long L_exact_log2$lazy_ptr-(L_exact_log2$scv+8)
4040
41 ; PIC: .lazy_symbol_pointer
41 ; PIC: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
4242 ; PIC: L_exact_log2$lazy_ptr:
4343 ; PIC: .indirect_symbol _exact_log2
4444 ; PIC: .long dyld_stub_binding_helper
5454 ; DYNAMIC: L_exact_log2$slp:
5555 ; DYNAMIC: .long L_exact_log2$lazy_ptr
5656
57 ; DYNAMIC: .lazy_symbol_pointer
57 ; DYNAMIC: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
5858 ; DYNAMIC: L_exact_log2$lazy_ptr:
5959 ; DYNAMIC: .indirect_symbol _exact_log2
6060 ; DYNAMIC: .long dyld_stub_binding_helper
4343 ; PIC: mtctr r12
4444 ; PIC: bctr
4545
46 ; PIC: .lazy_symbol_pointer
46 ; PIC: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
4747 ; PIC: L_exact_log2$lazy_ptr:
4848 ; PIC: .indirect_symbol _exact_log2
4949 ; PIC: .long dyld_stub_binding_helper
5959 ; DYNAMIC: mtctr r12
6060 ; DYNAMIC: bctr
6161
62 ; DYNAMIC: .lazy_symbol_pointer
62 ; DYNAMIC: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
6363 ; DYNAMIC: L_exact_log2$lazy_ptr:
6464 ; DYNAMIC: .indirect_symbol _exact_log2
6565 ; DYNAMIC: .long dyld_stub_binding_helper
None ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin | grep {\\.cstring} | count 1
1 %struct.A = type { }
2 %struct.NSString = type opaque
3 %struct.__builtin_CFString = type { i32*, i32, i8*, i32 }
4 %struct._objc_module = type { i32, i32, i8*, %struct._objc_symtab* }
5 %struct._objc_symtab = type { i32, %struct.objc_selector**, i16, i16 }
6 %struct.objc_object = type opaque
7 %struct.objc_selector = type opaque
8 @"\01L_unnamed_cfstring_0" = internal constant %struct.__builtin_CFString { i32* getelementptr ([0 x i32]* @__CFConstantStringClassReference, i32 0, i32 0), i32 1992, i8* getelementptr ([1 x i8]* @"\01LC", i32 0, i32 0), i32 0 }, section "__DATA, __cfstring" ; <%struct.__builtin_CFString*> [#uses=1]
9 @__CFConstantStringClassReference = external global [0 x i32] ; <[0 x i32]*> [#uses=1]
10 @"\01LC" = internal constant [1 x i8] zeroinitializer ; <[1 x i8]*> [#uses=1]
11 @"\01L_OBJC_SELECTOR_REFERENCES_0" = internal global %struct.objc_selector* bitcast ([6 x i8]* @"\01L_OBJC_METH_VAR_NAME_0" to %struct.objc_selector*), section "__OBJC,__message_refs,literal_pointers,no_dead_strip", align 4 ; <%struct.objc_selector**> [#uses=2]
12 @"\01L_OBJC_SYMBOLS" = internal global %struct._objc_symtab zeroinitializer, section "__OBJC,__symbols,regular,no_dead_strip", align 4 ; <%struct._objc_symtab*> [#uses=2]
13 @"\01L_OBJC_METH_VAR_NAME_0" = internal global [6 x i8] c"bork:\00", section "__TEXT,__cstring,cstring_literals", align 1 ; <[6 x i8]*> [#uses=2]
14 @"\01L_OBJC_IMAGE_INFO" = internal constant [2 x i32] zeroinitializer, section "__OBJC, __image_info,regular" ; <[2 x i32]*> [#uses=1]
15 @"\01L_OBJC_CLASS_NAME_0" = internal global [1 x i8] zeroinitializer, section "__TEXT,__cstring,cstring_literals", align 1 ; <[1 x i8]*> [#uses=1]
16 @"\01L_OBJC_MODULES" = internal global %struct._objc_module { i32 7, i32 16, i8* getelementptr ([1 x i8]* @"\01L_OBJC_CLASS_NAME_0", i32 0, i32 0), %struct._objc_symtab* @"\01L_OBJC_SYMBOLS" }, section "__OBJC,__module_info,regular,no_dead_strip", align 4 ; <%struct._objc_module*> [#uses=1]
17 @llvm.used = appending global [6 x i8*] [ i8* bitcast (%struct.objc_selector** @"\01L_OBJC_SELECTOR_REFERENCES_0" to i8*), i8* bitcast (%struct._objc_symtab* @"\01L_OBJC_SYMBOLS" to i8*), i8* getelementptr ([6 x i8]* @"\01L_OBJC_METH_VAR_NAME_0", i32 0, i32 0), i8* bitcast ([2 x i32]* @"\01L_OBJC_IMAGE_INFO" to i8*), i8* getelementptr ([1 x i8]* @"\01L_OBJC_CLASS_NAME_0", i32 0, i32 0), i8* bitcast (%struct._objc_module* @"\01L_OBJC_MODULES" to i8*) ], section "llvm.metadata" ; <[6 x i8*]*> [#uses=0]
0 ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin | FileCheck %s
1 ; CHECK: .section __TEXT,__cstring,cstring_literals
182
19 define void @func(%struct.A* %a) nounwind {
20 entry:
21 %a_addr = alloca %struct.A* ; <%struct.A**> [#uses=2]
22 %a.0 = alloca %struct.objc_object* ; <%struct.objc_object**> [#uses=2]
23 %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
24 store %struct.A* %a, %struct.A** %a_addr
25 %0 = load %struct.A** %a_addr, align 4 ; <%struct.A*> [#uses=1]
26 %1 = bitcast %struct.A* %0 to %struct.objc_object* ; <%struct.objc_object*> [#uses=1]
27 store %struct.objc_object* %1, %struct.objc_object** %a.0, align 4
28 %2 = load %struct.objc_selector** @"\01L_OBJC_SELECTOR_REFERENCES_0", align 4 ; <%struct.objc_selector*> [#uses=1]
29 %3 = load %struct.objc_object** %a.0, align 4 ; <%struct.objc_object*> [#uses=1]
30 call void bitcast (%struct.objc_object* (%struct.objc_object*, %struct.objc_selector*, ...)* @objc_msgSend to void (%struct.objc_object*, %struct.objc_selector*, %struct.NSString*)*)(%struct.objc_object* %3, %struct.objc_selector* %2, %struct.NSString* bitcast (%struct.__builtin_CFString* @"\01L_unnamed_cfstring_0" to %struct.NSString*)) nounwind
31 br label %return
3 @x = internal constant [1 x i8] zeroinitializer ; <[1 x i8]*> [#uses=1]
324
33 return: ; preds = %entry
34 ret void
35 }
5 @y = global [1 x i8]* @x
366
37 declare %struct.objc_object* @objc_msgSend(%struct.objc_object*, %struct.objc_selector*, ...)
2727 ; int * const G3 = &G1;
2828 @G3 = constant i32* @G1
2929
30 ; DARWIN: .const_data
30 ; DARWIN: .section __DATA,__const
3131 ; DARWIN: .globl _G3
3232 ; DARWIN: _G3:
3333 ; DARWIN: .long _G1
3636 ; _Complex long long const G4 = 34;
3737 @G4 = constant {i64,i64} { i64 34, i64 0 }
3838
39 ; DARWIN: .const
39 ; DARWIN: .section __TEXT,__const
4040 ; DARWIN: _G4:
4141 ; DARWIN: .long 34
4242
4949 ; LINUX: G5:
5050 ; LINUX: .long 47
5151
52 ; DARWIN: .data
52 ; DARWIN: .section __DATA,__data
5353 ; DARWIN: .globl _G5
5454 ; DARWIN: _G5:
5555 ; DARWIN: .long 47
8787
8888 @G7 = constant [10 x i8] c"abcdefghi\00"
8989
90 ; DARWIN: .cstring
90 ; DARWIN: __TEXT,__cstring,cstring_literals
9191 ; DARWIN: .globl _G7
9292 ; DARWIN: _G7:
9393 ; DARWIN: .asciz "abcdefghi"
110110
111111 @G9 = constant [4 x i32] [ i32 1, i32 2, i32 3, i32 0 ]
112112
113 ; DARWIN: .const
113 ; DARWIN: .section __TEXT,__const
114114 ; DARWIN: .globl _G9
115115 ; DARWIN: _G9:
116116
241241 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
242242 }
243243
244 StringRef AsmLexer::LexUntilEndOfStatement() {
245 TokStart = CurPtr;
246
247 while (*CurPtr != '#' && // Start of line comment.
248 *CurPtr != ';' && // End of statement marker.
249 *CurPtr != '\n' &&
250 *CurPtr != '\r' &&
251 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd()))
252 ++CurPtr;
253 return StringRef(TokStart, CurPtr-TokStart);
254 }
244255
245256 AsmToken AsmLexer::LexToken() {
246257 TokStart = CurPtr;
4949 ~AsmLexer();
5050
5151 SMLoc getLoc() const;
52
53 StringRef LexUntilEndOfStatement();
54
5255
5356 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
5457 bool EnterIncludeFile(const std::string &Filename);
403403 if (IDVal == ".text")
404404 // FIXME: This changes behavior based on the -static flag to the
405405 // assembler.
406 return ParseDirectiveSectionSwitch("__TEXT,__text",
407 "regular,pure_instructions");
406 return ParseDirectiveSectionSwitch("__TEXT", "__text",
407 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
408408 if (IDVal == ".const")
409 return ParseDirectiveSectionSwitch("__TEXT,__const");
409 return ParseDirectiveSectionSwitch("__TEXT", "__const", 0);
410410 if (IDVal == ".static_const")
411 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
411 return ParseDirectiveSectionSwitch("__TEXT", "__static_const", 0);
412412 if (IDVal == ".cstring")
413 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
414 "cstring_literals");
413 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
414 MCSectionMachO::S_CSTRING_LITERALS);
415415 if (IDVal == ".literal4")
416 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
416 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
417 MCSectionMachO::S_4BYTE_LITERALS);
417418 if (IDVal == ".literal8")
418 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
419 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
420 MCSectionMachO::S_8BYTE_LITERALS);
419421 if (IDVal == ".literal16")
420 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
421 "16byte_literals");
422 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
423 MCSectionMachO::S_16BYTE_LITERALS);
422424 if (IDVal == ".constructor")
423 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
425 return ParseDirectiveSectionSwitch("__TEXT","__constructor", 0);
424426 if (IDVal == ".destructor")
425 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
427 return ParseDirectiveSectionSwitch("__TEXT","__destructor", 0);
426428 if (IDVal == ".fvmlib_init0")
427 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
429 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0", 0);
428430 if (IDVal == ".fvmlib_init1")
429 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
430 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
431 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
432 "self_modifying_code+pure_instructions,5");
431 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1", 0);
432 if (IDVal == ".symbol_stub")
433 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
434 MCSectionMachO::S_SYMBOL_STUBS |
435 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
436 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
437 // FIXME: Different on PPC and ARM.
438 16);
433439 // FIXME: .picsymbol_stub on PPC.
434440 if (IDVal == ".data")
435 return ParseDirectiveSectionSwitch("__DATA,__data");
441 return ParseDirectiveSectionSwitch("__DATA", "__data", 0);
436442 if (IDVal == ".static_data")
437 return ParseDirectiveSectionSwitch("__DATA,__static_data");
443 return ParseDirectiveSectionSwitch("__DATA", "__static_data", 0);
438444 if (IDVal == ".non_lazy_symbol_pointer")
439 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
440 "non_lazy_symbol_pointers");
445 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_pointer",
446 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS);
441447 if (IDVal == ".lazy_symbol_pointer")
442 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
443 "lazy_symbol_pointers");
448 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_pointer",
449 MCSectionMachO::S_LAZY_SYMBOL_POINTERS);
444450 if (IDVal == ".dyld")
445 return ParseDirectiveSectionSwitch("__DATA,__dyld");
451 return ParseDirectiveSectionSwitch("__DATA", "__dyld", 0);
446452 if (IDVal == ".mod_init_func")
447 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
448 "mod_init_funcs");
453 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
454 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS);
449455 if (IDVal == ".mod_term_func")
450 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
451 "mod_term_funcs");
456 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
457 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS);
452458 if (IDVal == ".const_data")
453 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
459 return ParseDirectiveSectionSwitch("__DATA", "__const", 0);
454460
455461
456462 // FIXME: Verify attributes on sections.
457463 if (IDVal == ".objc_class")
458 return ParseDirectiveSectionSwitch("__OBJC,__class");
464 return ParseDirectiveSectionSwitch("__OBJC", "__class", 0);
459465 if (IDVal == ".objc_meta_class")
460 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
466 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", 0);
461467 if (IDVal == ".objc_cat_cls_meth")
462 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
468 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", 0);
463469 if (IDVal == ".objc_cat_inst_meth")
464 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
470 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", 0);
465471 if (IDVal == ".objc_protocol")
466 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
472 return ParseDirectiveSectionSwitch("__OBJC", "__protocol", 0);
467473 if (IDVal == ".objc_string_object")
468 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
474 return ParseDirectiveSectionSwitch("__OBJC", "__string_object", 0);
469475 if (IDVal == ".objc_cls_meth")
470 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
476 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", 0);
471477 if (IDVal == ".objc_inst_meth")
472 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
478 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", 0);
473479 if (IDVal == ".objc_cls_refs")
474 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
480 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", 0);
475481 if (IDVal == ".objc_message_refs")
476 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
482 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", 0);
477483 if (IDVal == ".objc_symbols")
478 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
484 return ParseDirectiveSectionSwitch("__OBJC", "__symbols", 0);
479485 if (IDVal == ".objc_category")
480 return ParseDirectiveSectionSwitch("__OBJC,__category");
486 return ParseDirectiveSectionSwitch("__OBJC", "__category", 0);
481487 if (IDVal == ".objc_class_vars")
482 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
488 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", 0);
483489 if (IDVal == ".objc_instance_vars")
484 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
490 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", 0);
485491 if (IDVal == ".objc_module_info")
486 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
492 return ParseDirectiveSectionSwitch("__OBJC", "__module_info", 0);
487493 if (IDVal == ".objc_class_names")
488 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
494 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
495 MCSectionMachO::S_CSTRING_LITERALS);
489496 if (IDVal == ".objc_meth_var_types")
490 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
497 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
498 MCSectionMachO::S_CSTRING_LITERALS);
491499 if (IDVal == ".objc_meth_var_names")
492 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
500 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
501 MCSectionMachO::S_CSTRING_LITERALS);
493502 if (IDVal == ".objc_selector_strs")
494 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
503 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", 0);
495504
496505 // Assembler features
497506 if (IDVal == ".set")
680689 bool AsmParser::ParseDirectiveDarwinSection() {
681690 StringRef SectionName;
682691
683 if (ParseIdentifier(SectionName))
692 if (Lexer.isNot(AsmToken::Identifier))
684693 return TokError("expected identifier after '.section' directive");
685694
686 std::string Section = SectionName;
687
688 // FIXME: This doesn't work, we lose quoting on things
689
690 // Accept a comma separated list of modifiers.
691 while (Lexer.is(AsmToken::Comma)) {
692 Lexer.Lex(); // Consume the comma.
693
694 StringRef ModifierName;
695 if (ParseIdentifier(ModifierName))
696 return TokError("expected identifier in '.section' directive");
697 Section += ',';
698 Section += ModifierName;
699 }
700
695 std::string SectionSpec = SectionName;
696 StringRef EOL = Lexer.LexUntilEndOfStatement();
697 SectionSpec.append(EOL.begin(), EOL.end());
698 Lexer.Lex();
701699 if (Lexer.isNot(AsmToken::EndOfStatement))
702700 return TokError("unexpected token in '.section' directive");
703701 Lexer.Lex();
704702
703
704 StringRef Segment, Section;
705 unsigned TAA, StubSize;
706 std::string ErrorStr =
707 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
708 TAA, StubSize);
709
710 if (!ErrorStr.empty())
711 return TokError(ErrorStr.c_str());
712
713 // FIXME: CACHE THESE.
714
705715 // FIXME: Arch specific.
706 MCSection *S = Ctx.GetSection(Section);
716 MCSection *S = 0; //Ctx.GetSection(Section);
707717 if (S == 0)
708 S = MCSectionMachO::Create(Section, false, SectionKind(), Ctx);
718 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
719 SectionKind(), Ctx);
709720
710721 Out.SwitchSection(S);
711722 return false;
712723 }
713724
714 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
715 const char *Directives) {
725 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
726 const char *Section,
727 unsigned TAA, unsigned StubSize) {
716728 if (Lexer.isNot(AsmToken::EndOfStatement))
717729 return TokError("unexpected token in section switching directive");
718730 Lexer.Lex();
719731
720 std::string SectionStr = Section;
721 if (Directives && Directives[0]) {
722 SectionStr += ",";
723 SectionStr += Directives;
724 }
725
726732 // FIXME: Arch specific.
727 MCSection *S = Ctx.GetSection(Section);
733 // FIXME: Cache this!
734 MCSection *S = 0; // Ctx.GetSection(Section);
728735 if (S == 0)
729 S = MCSectionMachO::Create(Section, false, SectionKind(), Ctx);
736 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
737 SectionKind(), Ctx);
730738
731739 Out.SwitchSection(S);
732740 return false;
10891097
10901098 if (Lexer.isNot(AsmToken::Identifier))
10911099 return TokError("expected segment name after '.zerofill' directive");
1092 std::string Section = Lexer.getTok().getString();
1100 StringRef Segment = Lexer.getTok().getString();
10931101 Lexer.Lex();
10941102
10951103 if (Lexer.isNot(AsmToken::Comma))
10961104 return TokError("unexpected token in directive");
1097 Section += ',';
10981105 Lexer.Lex();
10991106
11001107 if (Lexer.isNot(AsmToken::Identifier))
11011108 return TokError("expected section name after comma in '.zerofill' "
11021109 "directive");
1103 Section += Lexer.getTok().getString().str();
1104 Lexer.Lex();
1105
1106 // FIXME: we will need to tell GetSection() that this is to be created with or
1107 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1108 // below could be done but for now it is not as EmitZerofill() does not know
1109 // how to deal with a section type in the section name like
1110 // ParseDirectiveDarwinSection() allows.
1111 // Section += ',';
1112 // Section += "zerofill";
1110 StringRef Section = Lexer.getTok().getString();
1111 Lexer.Lex();
11131112
11141113 // If this is the end of the line all that was wanted was to create the
11151114 // the section but with no symbol.
11161115 if (Lexer.is(AsmToken::EndOfStatement)) {
1117 // FIXME: Arch specific.
1118 MCSection *S = Ctx.GetSection(Section);
1116 // FIXME: CACHE THIS.
1117 MCSection *S = 0; //Ctx.GetSection(Section);
11191118 if (S == 0)
1120 S = MCSectionMachO::Create(Section, false, SectionKind(), Ctx);
1119 S = MCSectionMachO::Create(Segment, Section,
1120 MCSectionMachO::S_ZEROFILL, 0,
1121 SectionKind(), Ctx);
11211122
11221123 // Create the zerofill section but no symbol
11231124 Out.EmitZerofill(S);
11751176 return Error(IDLoc, "invalid symbol redefinition");
11761177
11771178 // FIXME: Arch specific.
1178 MCSection *S = Ctx.GetSection(Section);
1179 // FIXME: CACHE.
1180 MCSection *S = 0; //Ctx.GetSection(Section);
11791181 if (S == 0)
1180 S = MCSectionMachO::Create(Section, false, SectionKind(), Ctx);
1182 S = MCSectionMachO::Create(Segment, Section,
1183 MCSectionMachO::S_ZEROFILL, 0,
1184 SectionKind(), Ctx);
11811185
11821186 // Create the zerofill Symbol with Size and Pow2Alignment
11831187 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
9898
9999 // Directive Parsing.
100100 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
101 bool ParseDirectiveSectionSwitch(const char *Section,
102 const char *Directives = 0);
101 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
102 unsigned TAA, unsigned StubSize = 0);
103103 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
104104 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
105105 bool ParseDirectiveFill(); // ".fill"
185185 OwningPtr Str(createAsmStreamer(Ctx, outs()));
186186
187187 // FIXME: Target hook & command line option for initial section.
188 Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT,__text,"
189 "regular,pure_instructions",
190 false,
191 SectionKind::getText(),
192 Ctx));
188 Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT","__text",
189 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
190 0, SectionKind::getText(),
191 Ctx));
193192
194193 AsmParser Parser(SrcMgr, Ctx, *Str.get());
195194 OwningPtr TAP(GetTargetAsmParser(ProgName, Parser));