blob: 291e40d1f45d9c6fce3f2e6ecdd33d9dc8bb1e3b [file] [log] [blame]
Chris Lattnered47a042009-07-31 17:02:00 +00001//===- lib/MC/MCSection.cpp - Machine 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/MCSection.h"
11#include "llvm/MC/MCContext.h"
Chris Lattner892e1822009-08-08 22:41:53 +000012#include "llvm/Target/TargetAsmInfo.h"
13#include "llvm/Support/raw_ostream.h"
Chris Lattnered47a042009-07-31 17:02:00 +000014using namespace llvm;
15
Chris Lattner892e1822009-08-08 22:41:53 +000016//===----------------------------------------------------------------------===//
17// MCSection
18//===----------------------------------------------------------------------===//
19
Chris Lattnered47a042009-07-31 17:02:00 +000020MCSection::~MCSection() {
21}
22
Chris Lattnered47a042009-07-31 17:02:00 +000023
Chris Lattner892e1822009-08-08 22:41:53 +000024//===----------------------------------------------------------------------===//
25// MCSectionELF
26//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +000027
28MCSectionELF *MCSectionELF::
29Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
30 return new (Ctx) MCSectionELF(Name, IsDirective, K, Ctx);
31}
32
Chris Lattner93b6db32009-08-08 23:39:42 +000033MCSectionELF::MCSectionELF(const StringRef &name, bool isDirective,
34 SectionKind K, MCContext &Ctx)
35 : MCSection(K), Name(name), IsDirective(isDirective) {
36 Ctx.SetSection(Name, this);
37}
38
39
Chris Lattner892e1822009-08-08 22:41:53 +000040void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
41 raw_ostream &OS) const {
42 if (isDirective()) {
43 OS << getName() << '\n';
44 return;
45 }
46
47 OS << "\t.section\t" << getName();
48
49 // Handle the weird solaris syntax if desired.
50 if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
51 !getKind().isMergeableConst() && !getKind().isMergeableCString()) {
52 if (!getKind().isMetadata())
53 OS << ",#alloc";
54 if (getKind().isText())
55 OS << ",#execinstr";
56 if (getKind().isWriteable())
57 OS << ",#write";
58 if (getKind().isThreadLocal())
59 OS << ",#tls";
60 } else {
61 OS << ",\"";
62
63 if (!getKind().isMetadata())
64 OS << 'a';
65 if (getKind().isText())
66 OS << 'x';
67 if (getKind().isWriteable())
68 OS << 'w';
69 if (getKind().isMergeable1ByteCString() ||
70 getKind().isMergeable2ByteCString() ||
71 getKind().isMergeable4ByteCString() ||
72 getKind().isMergeableConst4() ||
73 getKind().isMergeableConst8() ||
74 getKind().isMergeableConst16())
75 OS << 'M';
76 if (getKind().isMergeable1ByteCString() ||
77 getKind().isMergeable2ByteCString() ||
78 getKind().isMergeable4ByteCString())
79 OS << 'S';
80 if (getKind().isThreadLocal())
81 OS << 'T';
82
83 OS << "\",";
84
85 // If comment string is '@', e.g. as on ARM - use '%' instead
86 if (TAI.getCommentString()[0] == '@')
87 OS << '%';
88 else
89 OS << '@';
90
91 if (getKind().isBSS() || getKind().isThreadBSS())
92 OS << "nobits";
93 else
94 OS << "progbits";
95
96 if (getKind().isMergeable1ByteCString()) {
97 OS << ",1";
98 } else if (getKind().isMergeable2ByteCString()) {
99 OS << ",2";
100 } else if (getKind().isMergeable4ByteCString()) {
101 OS << ",4";
102 } else if (getKind().isMergeableConst4()) {
103 OS << ",4";
104 } else if (getKind().isMergeableConst8()) {
105 OS << ",8";
106 } else if (getKind().isMergeableConst16()) {
107 OS << ",16";
108 }
109 }
Chris Lattner1f8e8db2009-08-09 15:31:10 +0000110
111 OS << '\n';
Chris Lattner892e1822009-08-08 22:41:53 +0000112}
113
114//===----------------------------------------------------------------------===//
115// MCSectionMachO
116//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +0000117
Chris Lattnerff4bc462009-08-10 01:39:42 +0000118/// SectionTypeDescriptors - These are strings that describe the various section
119/// types. This *must* be kept in order with and stay synchronized with the
120/// section type list.
121static const struct {
122 const char *AssemblerName, *EnumName;
123} SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
124 { "regular", "S_REGULAR" }, // 0x00
125 { 0, "S_ZEROFILL" }, // 0x01
126 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
127 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
128 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
129 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
130 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
131 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
132 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
133 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
134 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
135 { "coalesced", "S_COALESCED" }, // 0x0B
136 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
137 { "interposing", "S_INTERPOSING" }, // 0x0D
138 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
139 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
140 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10
141};
Chris Lattnera1c31b72009-08-08 20:50:49 +0000142
Chris Lattnerff4bc462009-08-10 01:39:42 +0000143
144/// SectionAttrDescriptors - This is an array of descriptors for section
145/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
146/// by attribute, instead it is searched. The last entry has a zero AttrFlag
147/// value.
148static const struct {
149 unsigned AttrFlag;
150 const char *AssemblerName, *EnumName;
151} SectionAttrDescriptors[] = {
152#define ENTRY(ASMNAME, ENUM) \
153 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
154ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
155ENTRY("no_toc", S_ATTR_NO_TOC)
156ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
157ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
158ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
159ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
160ENTRY("debug", S_ATTR_DEBUG)
161ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
162ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
163ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
164#undef ENTRY
165 { 0, "none", 0 }
166};
167
168
169MCSectionMachO *MCSectionMachO::
170Create(const StringRef &Segment, const StringRef &Section,
171 unsigned TypeAndAttributes, unsigned Reserved2,
172 SectionKind K, MCContext &Ctx) {
173 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero.
174 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
175 Reserved2, K);
Chris Lattner93b6db32009-08-08 23:39:42 +0000176}
177
Chris Lattner892e1822009-08-08 22:41:53 +0000178void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
179 raw_ostream &OS) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000180 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
181
182 // Get the section type and attributes.
183 unsigned TAA = getTypeAndAttributes();
184 if (TAA == 0) {
185 OS << '\n';
186 return;
187 }
188
189 OS << ',';
190
191 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
192 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
193 "Invalid SectionType specified!");
194
195 if (SectionTypeDescriptors[SectionType].AssemblerName)
196 OS << SectionTypeDescriptors[SectionType].AssemblerName;
Chris Lattner892e1822009-08-08 22:41:53 +0000197 else
Chris Lattnerff4bc462009-08-10 01:39:42 +0000198 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
199
200 // If we don't have any attributes, we're done.
201 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
202 if (SectionAttrs == 0) {
203 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
204 // the attribute specifier.
205 if (Reserved2 != 0)
206 OS << ",none," << Reserved2;
207 OS << '\n';
208 return;
209 }
210
211 // Check each attribute to see if we have it.
212 char Separator = ',';
213 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
214 // Check to see if we have this attribute.
215 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
216 continue;
217
218 // Yep, clear it and print it.
219 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
220
221 OS << Separator;
222 if (SectionAttrDescriptors[i].AssemblerName)
223 OS << SectionAttrDescriptors[i].AssemblerName;
224 else
225 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
226 Separator = '+';
227 }
228
229 assert(SectionAttrs == 0 && "Unknown section attributes!");
230
231 // If we have a S_SYMBOL_STUBS size specified, print it.
232 if (Reserved2 != 0)
233 OS << ',' << Reserved2;
234 OS << '\n';
Chris Lattner892e1822009-08-08 22:41:53 +0000235}
236
Chris Lattnerff4bc462009-08-10 01:39:42 +0000237/// StripSpaces - This removes leading and trailing spaces from the StringRef.
238static void StripSpaces(StringRef &Str) {
239 while (!Str.empty() && isspace(Str[0]))
240 Str = Str.substr(1);
241 while (!Str.empty() && isspace(Str.back()))
242 Str = Str.substr(0, Str.size()-1);
243}
244
245/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
246/// This is a string that can appear after a .section directive in a mach-o
247/// flavored .s file. If successful, this fills in the specified Out
248/// parameters and returns an empty string. When an invalid section
249/// specifier is present, this returns a string indicating the problem.
250std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
251 StringRef &Segment, // Out.
252 StringRef &Section, // Out.
253 unsigned &TAA, // Out.
254 unsigned &StubSize) { // Out.
255 // Find the first comma.
256 std::pair<StringRef, StringRef> Comma = Spec.split(',');
257
258 // If there is no comma, we fail.
259 if (Comma.second.empty())
260 return "mach-o section specifier requires a segment and section "
261 "separated by a comma";
262
263 // Capture segment, remove leading and trailing whitespace.
264 Segment = Comma.first;
265 StripSpaces(Segment);
266
267 // Verify that the segment is present and not too long.
268 if (Segment.empty() || Segment.size() > 16)
269 return "mach-o section specifier requires a segment whose length is "
270 "between 1 and 16 characters";
271
272 // Split the section name off from any attributes if present.
273 Comma = Comma.second.split(',');
274
275 // Capture section, remove leading and trailing whitespace.
276 Section = Comma.first;
277 StripSpaces(Section);
278
279 // Verify that the section is present and not too long.
280 if (Section.empty() || Section.size() > 16)
281 return "mach-o section specifier requires a section whose length is "
282 "between 1 and 16 characters";
283
284 // If there is no comma after the section, we're done.
285 TAA = 0;
286 StubSize = 0;
287 if (Comma.second.empty())
288 return "";
289
290 // Otherwise, we need to parse the section type and attributes.
291 Comma = Comma.second.split(',');
292
293 // Get the section type.
294 StringRef SectionType = Comma.first;
295 StripSpaces(SectionType);
296
297 // Figure out which section type it is.
298 unsigned TypeID;
299 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
300 if (SectionTypeDescriptors[TypeID].AssemblerName &&
301 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
302 break;
303
304 // If we didn't find the section type, reject it.
305 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
306 return "mach-o section specifier uses an unknown section type";
307
308 // Remember the TypeID.
309 TAA = TypeID;
310
311 // If we have no comma after the section type, there are no attributes.
312 if (Comma.second.empty()) {
313 // S_SYMBOL_STUBS always require a symbol stub size specifier.
314 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
315 return "mach-o section specifier of type 'symbol_stubs' requires a size "
316 "specifier";
317 return "";
318 }
319
320 // Otherwise, we do have some attributes. Split off the size specifier if
321 // present.
322 Comma = Comma.second.split(',');
323 StringRef Attrs = Comma.first;
324
325 // The attribute list is a '+' separated list of attributes.
326 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
327
328 while (1) {
329 StringRef Attr = Plus.first;
330 StripSpaces(Attr);
331
332 // Look up the attribute.
333 for (unsigned i = 0; ; ++i) {
334 if (SectionAttrDescriptors[i].AttrFlag == 0)
335 return "mach-o section specifier has invalid attribute";
336
337 if (SectionAttrDescriptors[i].AssemblerName &&
338 Attr == SectionAttrDescriptors[i].AssemblerName) {
339 TAA |= SectionAttrDescriptors[i].AttrFlag;
340 break;
341 }
342 }
343
344 if (Plus.second.empty()) break;
345 Plus = Plus.second.split('+');
346 };
347
348 // Okay, we've parsed the section attributes, see if we have a stub size spec.
349 if (Comma.second.empty()) {
350 // S_SYMBOL_STUBS always require a symbol stub size specifier.
351 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
352 return "mach-o section specifier of type 'symbol_stubs' requires a size "
353 "specifier";
354 return "";
355 }
356
357 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
358 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
359 return "mach-o section specifier cannot have a stub size specified because "
360 "it does not have type 'symbol_stubs'";
361
362 // Okay, if we do, it must be a number.
363 StringRef StubSizeStr = Comma.second;
364 StripSpaces(StubSizeStr);
365
366 // Convert the a null terminated buffer for strtoul.
367 char TmpBuffer[32];
368 if (StubSizeStr.size() >= 32)
369 return"mach-o section specifier has a stub size specifier that is too long";
370
371 memcpy(TmpBuffer, StubSizeStr.data(), StubSizeStr.size());
372 TmpBuffer[StubSizeStr.size()] = 0;
373
374 char *EndPtr;
375 StubSize = strtoul(TmpBuffer, &EndPtr, 0);
376
377 if (EndPtr[0] != 0)
378 return "mach-o section specifier has a malformed stub size";
379
380 return "";
381}
382
383
Chris Lattner892e1822009-08-08 22:41:53 +0000384//===----------------------------------------------------------------------===//
385// MCSectionCOFF
386//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +0000387
Chris Lattner7c599d02009-08-08 20:52:13 +0000388MCSectionCOFF *MCSectionCOFF::
Chris Lattnera1c31b72009-08-08 20:50:49 +0000389Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
Chris Lattner7c599d02009-08-08 20:52:13 +0000390 return new (Ctx) MCSectionCOFF(Name, IsDirective, K, Ctx);
Chris Lattnera1c31b72009-08-08 20:50:49 +0000391}
392
Chris Lattner93b6db32009-08-08 23:39:42 +0000393MCSectionCOFF::MCSectionCOFF(const StringRef &name, bool isDirective,
394 SectionKind K, MCContext &Ctx)
395 : MCSection(K), Name(name), IsDirective(isDirective) {
396 Ctx.SetSection(Name, this);
397}
398
399
Chris Lattner892e1822009-08-08 22:41:53 +0000400void MCSectionCOFF::PrintSwitchToSection(const TargetAsmInfo &TAI,
401 raw_ostream &OS) const {
402
403 if (isDirective()) {
404 OS << getName() << '\n';
405 return;
406 }
407 OS << "\t.section\t" << getName() << ",\"";
408 if (getKind().isText())
409 OS << 'x';
410 if (getKind().isWriteable())
411 OS << 'w';
412 OS << "\"\n";
413}