add a new MachineModuleInfoMachO class, which is the per-module
[llvm/avr.git] / lib / MC / MCSectionMachO.cpp
blob32d908ff68520df48999925d8ff58434604e5be9
1 //===- 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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSectionMachO.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
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.
18 static 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
37 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10
41 /// SectionAttrDescriptors - This is an array of descriptors for section
42 /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
43 /// by attribute, instead it is searched. The last entry has a zero AttrFlag
44 /// value.
45 static const struct {
46 unsigned AttrFlag;
47 const char *AssemblerName, *EnumName;
48 } SectionAttrDescriptors[] = {
49 #define ENTRY(ASMNAME, ENUM) \
50 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
51 ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
52 ENTRY("no_toc", S_ATTR_NO_TOC)
53 ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
54 ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
55 ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
56 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
57 ENTRY("debug", S_ATTR_DEBUG)
58 ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
59 ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
60 ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
61 #undef ENTRY
62 { 0, "none", 0 }
66 MCSectionMachO *MCSectionMachO::
67 Create(const StringRef &Segment, const StringRef &Section,
68 unsigned TypeAndAttributes, unsigned Reserved2,
69 SectionKind K, MCContext &Ctx) {
70 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero.
71 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
72 Reserved2, K);
75 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
76 raw_ostream &OS) const {
77 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
79 // Get the section type and attributes.
80 unsigned TAA = getTypeAndAttributes();
81 if (TAA == 0) {
82 OS << '\n';
83 return;
86 OS << ',';
88 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
89 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
90 "Invalid SectionType specified!");
92 if (SectionTypeDescriptors[SectionType].AssemblerName)
93 OS << SectionTypeDescriptors[SectionType].AssemblerName;
94 else
95 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
97 // If we don't have any attributes, we're done.
98 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
99 if (SectionAttrs == 0) {
100 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
101 // the attribute specifier.
102 if (Reserved2 != 0)
103 OS << ",none," << Reserved2;
104 OS << '\n';
105 return;
108 // Check each attribute to see if we have it.
109 char Separator = ',';
110 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
111 // Check to see if we have this attribute.
112 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
113 continue;
115 // Yep, clear it and print it.
116 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
118 OS << Separator;
119 if (SectionAttrDescriptors[i].AssemblerName)
120 OS << SectionAttrDescriptors[i].AssemblerName;
121 else
122 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
123 Separator = '+';
126 assert(SectionAttrs == 0 && "Unknown section attributes!");
128 // If we have a S_SYMBOL_STUBS size specified, print it.
129 if (Reserved2 != 0)
130 OS << ',' << Reserved2;
131 OS << '\n';
134 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
135 static void StripSpaces(StringRef &Str) {
136 while (!Str.empty() && isspace(Str[0]))
137 Str = Str.substr(1);
138 while (!Str.empty() && isspace(Str.back()))
139 Str = Str.substr(0, Str.size()-1);
142 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
143 /// This is a string that can appear after a .section directive in a mach-o
144 /// flavored .s file. If successful, this fills in the specified Out
145 /// parameters and returns an empty string. When an invalid section
146 /// specifier is present, this returns a string indicating the problem.
147 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
148 StringRef &Segment, // Out.
149 StringRef &Section, // Out.
150 unsigned &TAA, // Out.
151 unsigned &StubSize) { // Out.
152 // Find the first comma.
153 std::pair<StringRef, StringRef> Comma = Spec.split(',');
155 // If there is no comma, we fail.
156 if (Comma.second.empty())
157 return "mach-o section specifier requires a segment and section "
158 "separated by a comma";
160 // Capture segment, remove leading and trailing whitespace.
161 Segment = Comma.first;
162 StripSpaces(Segment);
164 // Verify that the segment is present and not too long.
165 if (Segment.empty() || Segment.size() > 16)
166 return "mach-o section specifier requires a segment whose length is "
167 "between 1 and 16 characters";
169 // Split the section name off from any attributes if present.
170 Comma = Comma.second.split(',');
172 // Capture section, remove leading and trailing whitespace.
173 Section = Comma.first;
174 StripSpaces(Section);
176 // Verify that the section is present and not too long.
177 if (Section.empty() || Section.size() > 16)
178 return "mach-o section specifier requires a section whose length is "
179 "between 1 and 16 characters";
181 // If there is no comma after the section, we're done.
182 TAA = 0;
183 StubSize = 0;
184 if (Comma.second.empty())
185 return "";
187 // Otherwise, we need to parse the section type and attributes.
188 Comma = Comma.second.split(',');
190 // Get the section type.
191 StringRef SectionType = Comma.first;
192 StripSpaces(SectionType);
194 // Figure out which section type it is.
195 unsigned TypeID;
196 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
197 if (SectionTypeDescriptors[TypeID].AssemblerName &&
198 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
199 break;
201 // If we didn't find the section type, reject it.
202 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
203 return "mach-o section specifier uses an unknown section type";
205 // Remember the TypeID.
206 TAA = TypeID;
208 // If we have no comma after the section type, there are no attributes.
209 if (Comma.second.empty()) {
210 // S_SYMBOL_STUBS always require a symbol stub size specifier.
211 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
212 return "mach-o section specifier of type 'symbol_stubs' requires a size "
213 "specifier";
214 return "";
217 // Otherwise, we do have some attributes. Split off the size specifier if
218 // present.
219 Comma = Comma.second.split(',');
220 StringRef Attrs = Comma.first;
222 // The attribute list is a '+' separated list of attributes.
223 std::pair<StringRef, StringRef> Plus = Attrs.split('+');
225 while (1) {
226 StringRef Attr = Plus.first;
227 StripSpaces(Attr);
229 // Look up the attribute.
230 for (unsigned i = 0; ; ++i) {
231 if (SectionAttrDescriptors[i].AttrFlag == 0)
232 return "mach-o section specifier has invalid attribute";
234 if (SectionAttrDescriptors[i].AssemblerName &&
235 Attr == SectionAttrDescriptors[i].AssemblerName) {
236 TAA |= SectionAttrDescriptors[i].AttrFlag;
237 break;
241 if (Plus.second.empty()) break;
242 Plus = Plus.second.split('+');
245 // Okay, we've parsed the section attributes, see if we have a stub size spec.
246 if (Comma.second.empty()) {
247 // S_SYMBOL_STUBS always require a symbol stub size specifier.
248 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
249 return "mach-o section specifier of type 'symbol_stubs' requires a size "
250 "specifier";
251 return "";
254 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
255 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
256 return "mach-o section specifier cannot have a stub size specified because "
257 "it does not have type 'symbol_stubs'";
259 // Okay, if we do, it must be a number.
260 StringRef StubSizeStr = Comma.second;
261 StripSpaces(StubSizeStr);
263 // Convert the a null terminated buffer for strtoul.
264 char TmpBuffer[32];
265 if (StubSizeStr.size() >= 32)
266 return"mach-o section specifier has a stub size specifier that is too long";
268 memcpy(TmpBuffer, StubSizeStr.data(), StubSizeStr.size());
269 TmpBuffer[StubSizeStr.size()] = 0;
271 char *EndPtr;
272 StubSize = strtoul(TmpBuffer, &EndPtr, 0);
274 if (EndPtr[0] != 0)
275 return "mach-o section specifier has a malformed stub size";
277 return "";