llvm.org GIT mirror llvm / f9bdedd
split MachO section handling stuff out to its out .h/.cpp file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78576 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
12 changed file(s) with 462 addition(s) and 429 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.
73 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
22269 class MCSectionCOFF : public MCSection {
22370 std::string Name;
22471
0 //===- MCSectionMachO.h - MachO Machine Code Sections -----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the MCSectionMachO class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_MC_MCSECTIONMACHO_H
14 #define LLVM_MC_MCSECTIONMACHO_H
15
16 #include "llvm/MC/MCSection.h"
17
18 namespace llvm {
19
20 /// MCSectionMachO - This represents a section on a Mach-O system (used by
21 /// Mac OS X). On a Mac system, these are also described in
22 /// /usr/include/mach-o/loader.h.
23 class MCSectionMachO : public MCSection {
24 char SegmentName[16]; // Not necessarily null terminated!
25 char SectionName[16]; // Not necessarily null terminated!
26
27 /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
28 /// field of a section, drawn from the enums below.
29 unsigned TypeAndAttributes;
30
31 /// Reserved2 - The 'reserved2' field of a section, used to represent the
32 /// size of stubs, for example.
33 unsigned Reserved2;
34
35 MCSectionMachO(const StringRef &Segment, const StringRef &Section,
36 unsigned TAA, unsigned reserved2, SectionKind K)
37 : MCSection(K), TypeAndAttributes(TAA), Reserved2(reserved2) {
38 assert(Segment.size() <= 16 && Section.size() <= 16 &&
39 "Segment or section string too long");
40 for (unsigned i = 0; i != 16; ++i) {
41 if (i < Segment.size())
42 SegmentName[i] = Segment[i];
43 else
44 SegmentName[i] = 0;
45
46 if (i < Section.size())
47 SectionName[i] = Section[i];
48 else
49 SectionName[i] = 0;
50 }
51 }
52 public:
53
54 static MCSectionMachO *Create(const StringRef &Segment,
55 const StringRef &Section,
56 unsigned TypeAndAttributes,
57 unsigned Reserved2,
58 SectionKind K, MCContext &Ctx);
59
60 /// These are the section type and attributes fields. A MachO section can
61 /// have only one Type, but can have any of the attributes specified.
62 enum {
63 // TypeAndAttributes bitmasks.
64 SECTION_TYPE = 0x000000FFU,
65 SECTION_ATTRIBUTES = 0xFFFFFF00U,
66
67 // Valid section types.
68
69 /// S_REGULAR - Regular section.
70 S_REGULAR = 0x00U,
71 /// S_ZEROFILL - Zero fill on demand section.
72 S_ZEROFILL = 0x01U,
73 /// S_CSTRING_LITERALS - Section with literal C strings.
74 S_CSTRING_LITERALS = 0x02U,
75 /// S_4BYTE_LITERALS - Section with 4 byte literals.
76 S_4BYTE_LITERALS = 0x03U,
77 /// S_8BYTE_LITERALS - Section with 8 byte literals.
78 S_8BYTE_LITERALS = 0x04U,
79 /// S_LITERAL_POINTERS - Section with pointers to literals.
80 S_LITERAL_POINTERS = 0x05U,
81 /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
82 S_NON_LAZY_SYMBOL_POINTERS = 0x06U,
83 /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
84 S_LAZY_SYMBOL_POINTERS = 0x07U,
85 /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
86 /// the Reserved2 field.
87 S_SYMBOL_STUBS = 0x08U,
88 /// S_SYMBOL_STUBS - Section with only function pointers for
89 /// initialization.
90 S_MOD_INIT_FUNC_POINTERS = 0x09U,
91 /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
92 /// termination.
93 S_MOD_TERM_FUNC_POINTERS = 0x0AU,
94 /// S_COALESCED - Section contains symbols that are to be coalesced.
95 S_COALESCED = 0x0BU,
96 /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
97 /// gigabytes).
98 S_GB_ZEROFILL = 0x0CU,
99 /// S_INTERPOSING - Section with only pairs of function pointers for
100 /// interposing.
101 S_INTERPOSING = 0x0DU,
102 /// S_16BYTE_LITERALS - Section with only 16 byte literals.
103 S_16BYTE_LITERALS = 0x0EU,
104 /// S_DTRACE_DOF - Section contains DTrace Object Format.
105 S_DTRACE_DOF = 0x0FU,
106 /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
107 /// lazy loaded dylibs.
108 S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
109
110 LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS,
111
112
113 // Valid section attributes.
114
115 /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
116 /// instructions.
117 S_ATTR_PURE_INSTRUCTIONS = 1U << 31,
118 /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
119 /// in a ranlib table of contents.
120 S_ATTR_NO_TOC = 1U << 30,
121 /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
122 /// in files with the MY_DYLDLINK flag.
123 S_ATTR_STRIP_STATIC_SYMS = 1U << 29,
124 /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
125 S_ATTR_NO_DEAD_STRIP = 1U << 28,
126 /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
127 S_ATTR_LIVE_SUPPORT = 1U << 27,
128 /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
129 /// dyld.
130 S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
131 /// S_ATTR_DEBUG - A debug section.
132 S_ATTR_DEBUG = 1U << 25,
133 /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
134 S_ATTR_SOME_INSTRUCTIONS = 1U << 10,
135 /// S_ATTR_EXT_RELOC - Section has external relocation entries.
136 S_ATTR_EXT_RELOC = 1U << 9,
137 /// S_ATTR_LOC_RELOC - Section has local relocation entries.
138 S_ATTR_LOC_RELOC = 1U << 8
139 };
140
141 StringRef getSegmentName() const {
142 // SegmentName is not necessarily null terminated!
143 if (SegmentName[15])
144 return StringRef(SegmentName, 16);
145 return StringRef(SegmentName);
146 }
147 StringRef getSectionName() const {
148 // SectionName is not necessarily null terminated!
149 if (SectionName[15])
150 return StringRef(SectionName, 16);
151 return StringRef(SectionName);
152 }
153
154 unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
155
156
157 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
158 /// This is a string that can appear after a .section directive in a mach-o
159 /// flavored .s file. If successful, this fills in the specified Out
160 /// parameters and returns an empty string. When an invalid section
161 /// specifier is present, this returns a string indicating the problem.
162 static std::string ParseSectionSpecifier(StringRef Spec, // In.
163 StringRef &Segment, // Out.
164 StringRef &Section, // Out.
165 unsigned &TAA, // Out.
166 unsigned &StubSize); // Out.
167
168 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
169 raw_ostream &OS) const;
170 };
171
172 } // end namespace llvm
173
174 #endif
33 MCAsmStreamer.cpp
44 MCContext.cpp
55 MCSection.cpp
6 MCSectionMachO.cpp
67 MCStreamer.cpp
78 TargetAsmParser.cpp
89 )
1010
1111 #include "llvm/MC/MCContext.h"
1212 #include "llvm/MC/MCInst.h"
13 #include "llvm/MC/MCSection.h"
13 #include "llvm/MC/MCSectionMachO.h"
1414 #include "llvm/MC/MCSymbol.h"
1515 #include "llvm/MC/MCValue.h"
1616 #include "llvm/Support/ErrorHandling.h"
110110 OS << '\n';
111111 }
112112
113 //===----------------------------------------------------------------------===//
114 // MCSectionMachO
115 //===----------------------------------------------------------------------===//
116
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
168 MCSectionMachO *MCSectionMachO::
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);
175 }
176
177 void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
178 raw_ostream &OS) const {
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;
196 else
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
382113
383114 //===----------------------------------------------------------------------===//
384115 // MCSectionCOFF
0 //===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/MCSectionMachO.h"
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/Support/raw_ostream.h"
12 using namespace llvm;
13
14 /// SectionTypeDescriptors - These are strings that describe the various section
15 /// types. This *must* be kept in order with and stay synchronized with the
16 /// section type list.
17 static const struct {
18 const char *AssemblerName, *EnumName;
19 } SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
20 { "regular", "S_REGULAR" }, // 0x00
21 { 0, "S_ZEROFILL" }, // 0x01
22 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
23 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
24 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
25 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
26 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
27 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
28 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
29 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
30 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
31 { "coalesced", "S_COALESCED" }, // 0x0B
32 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
33 { "interposing", "S_INTERPOSING" }, // 0x0D
34 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
35 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
36 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10
37 };
38
39
40 /// SectionAttrDescriptors - This is an array of descriptors for section
41 /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
42 /// by attribute, instead it is searched. The last entry has a zero AttrFlag
43 /// value.
44 static const struct {
45 unsigned AttrFlag;
46 const char *AssemblerName, *EnumName;
47 } SectionAttrDescriptors[] = {
48 #define ENTRY(ASMNAME, ENUM) \
49 { MCSectionMachO::ENUM, ASMNAME, #ENUM },
50 ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
51 ENTRY("no_toc", S_ATTR_NO_TOC)
52 ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
53 ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
54 ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
55 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
56 ENTRY("debug", S_ATTR_DEBUG)
57 ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
58 ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
59 ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
60 #undef ENTRY
61 { 0, "none", 0 }
62 };
63
64
65 MCSectionMachO *MCSectionMachO::
66 Create(const StringRef &Segment, const StringRef &Section,
67 unsigned TypeAndAttributes, unsigned Reserved2,
68 SectionKind K, MCContext &Ctx) {
69 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero.
70 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
71 Reserved2, K);
72 }
73
74 void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
75 raw_ostream &OS) const {
76 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
77
78 // Get the section type and attributes.
79 unsigned TAA = getTypeAndAttributes();
80 if (TAA == 0) {
81 OS << '\n';
82 return;
83 }
84
85 OS << ',';
86
87 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
88 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
89 "Invalid SectionType specified!");
90
91 if (SectionTypeDescriptors[SectionType].AssemblerName)
92 OS << SectionTypeDescriptors[SectionType].AssemblerName;
93 else
94 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
95
96 // If we don't have any attributes, we're done.
97 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
98 if (SectionAttrs == 0) {
99 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
100 // the attribute specifier.
101 if (Reserved2 != 0)
102 OS << ",none," << Reserved2;
103 OS << '\n';
104 return;
105 }
106
107 // Check each attribute to see if we have it.
108 char Separator = ',';
109 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
110 // Check to see if we have this attribute.
111 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
112 continue;
113
114 // Yep, clear it and print it.
115 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
116
117 OS << Separator;
118 if (SectionAttrDescriptors[i].AssemblerName)
119 OS << SectionAttrDescriptors[i].AssemblerName;
120 else
121 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
122 Separator = '+';
123 }
124
125 assert(SectionAttrs == 0 && "Unknown section attributes!");
126
127 // If we have a S_SYMBOL_STUBS size specified, print it.
128 if (Reserved2 != 0)
129 OS << ',' << Reserved2;
130 OS << '\n';
131 }
132
133 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
134 static void StripSpaces(StringRef &Str) {
135 while (!Str.empty() && isspace(Str[0]))
136 Str = Str.substr(1);
137 while (!Str.empty() && isspace(Str.back()))
138 Str = Str.substr(0, Str.size()-1);
139 }
140
141 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
142 /// This is a string that can appear after a .section directive in a mach-o
143 /// flavored .s file. If successful, this fills in the specified Out
144 /// parameters and returns an empty string. When an invalid section
145 /// specifier is present, this returns a string indicating the problem.
146 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
147 StringRef &Segment, // Out.
148 StringRef &Section, // Out.
149 unsigned &TAA, // Out.
150 unsigned &StubSize) { // Out.
151 // Find the first comma.
152 std::pair Comma = Spec.split(',');
153
154 // If there is no comma, we fail.
155 if (Comma.second.empty())
156 return "mach-o section specifier requires a segment and section "
157 "separated by a comma";
158
159 // Capture segment, remove leading and trailing whitespace.
160 Segment = Comma.first;
161 StripSpaces(Segment);
162
163 // Verify that the segment is present and not too long.
164 if (Segment.empty() || Segment.size() > 16)
165 return "mach-o section specifier requires a segment whose length is "
166 "between 1 and 16 characters";
167
168 // Split the section name off from any attributes if present.
169 Comma = Comma.second.split(',');
170
171 // Capture section, remove leading and trailing whitespace.
172 Section = Comma.first;
173 StripSpaces(Section);
174
175 // Verify that the section is present and not too long.
176 if (Section.empty() || Section.size() > 16)
177 return "mach-o section specifier requires a section whose length is "
178 "between 1 and 16 characters";
179
180 // If there is no comma after the section, we're done.
181 TAA = 0;
182 StubSize = 0;
183 if (Comma.second.empty())
184 return "";
185
186 // Otherwise, we need to parse the section type and attributes.
187 Comma = Comma.second.split(',');
188
189 // Get the section type.
190 StringRef SectionType = Comma.first;
191 StripSpaces(SectionType);
192
193 // Figure out which section type it is.
194 unsigned TypeID;
195 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
196 if (SectionTypeDescriptors[TypeID].AssemblerName &&
197 SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
198 break;
199
200 // If we didn't find the section type, reject it.
201 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
202 return "mach-o section specifier uses an unknown section type";
203
204 // Remember the TypeID.
205 TAA = TypeID;
206
207 // If we have no comma after the section type, there are no attributes.
208 if (Comma.second.empty()) {
209 // S_SYMBOL_STUBS always require a symbol stub size specifier.
210 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
211 return "mach-o section specifier of type 'symbol_stubs' requires a size "
212 "specifier";
213 return "";
214 }
215
216 // Otherwise, we do have some attributes. Split off the size specifier if
217 // present.
218 Comma = Comma.second.split(',');
219 StringRef Attrs = Comma.first;
220
221 // The attribute list is a '+' separated list of attributes.
222 std::pair Plus = Attrs.split('+');
223
224 while (1) {
225 StringRef Attr = Plus.first;
226 StripSpaces(Attr);
227
228 // Look up the attribute.
229 for (unsigned i = 0; ; ++i) {
230 if (SectionAttrDescriptors[i].AttrFlag == 0)
231 return "mach-o section specifier has invalid attribute";
232
233 if (SectionAttrDescriptors[i].AssemblerName &&
234 Attr == SectionAttrDescriptors[i].AssemblerName) {
235 TAA |= SectionAttrDescriptors[i].AttrFlag;
236 break;
237 }
238 }
239
240 if (Plus.second.empty()) break;
241 Plus = Plus.second.split('+');
242 };
243
244 // Okay, we've parsed the section attributes, see if we have a stub size spec.
245 if (Comma.second.empty()) {
246 // S_SYMBOL_STUBS always require a symbol stub size specifier.
247 if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
248 return "mach-o section specifier of type 'symbol_stubs' requires a size "
249 "specifier";
250 return "";
251 }
252
253 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
254 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
255 return "mach-o section specifier cannot have a stub size specified because "
256 "it does not have type 'symbol_stubs'";
257
258 // Okay, if we do, it must be a number.
259 StringRef StubSizeStr = Comma.second;
260 StripSpaces(StubSizeStr);
261
262 // Convert the a null terminated buffer for strtoul.
263 char TmpBuffer[32];
264 if (StubSizeStr.size() >= 32)
265 return"mach-o section specifier has a stub size specifier that is too long";
266
267 memcpy(TmpBuffer, StubSizeStr.data(), StubSizeStr.size());
268 TmpBuffer[StubSizeStr.size()] = 0;
269
270 char *EndPtr;
271 StubSize = strtoul(TmpBuffer, &EndPtr, 0);
272
273 if (EndPtr[0] != 0)
274 return "mach-o section specifier has a malformed stub size";
275
276 return "";
277 }
278
2525 #include "llvm/CodeGen/MachineModuleInfo.h"
2626 #include "llvm/CodeGen/MachineFunctionPass.h"
2727 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionMachO.h"
2929 #include "llvm/Target/TargetAsmInfo.h"
3030 #include "llvm/Target/TargetData.h"
3131 #include "llvm/Target/TargetLoweringObjectFile.h"
3030 #include "llvm/CodeGen/MachineFunctionPass.h"
3131 #include "llvm/CodeGen/MachineInstr.h"
3232 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCSectionMachO.h"
3434 #include "llvm/Target/TargetAsmInfo.h"
3535 #include "llvm/Target/TargetLoweringObjectFile.h"
3636 #include "llvm/Target/TargetRegisterInfo.h"
1616 #include "llvm/DerivedTypes.h"
1717 #include "llvm/GlobalVariable.h"
1818 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSectionMachO.h"
2020 #include "llvm/Target/TargetAsmInfo.h"
2121 #include "llvm/Target/TargetData.h"
2222 #include "llvm/Target/TargetMachine.h"
2727 #include "llvm/ADT/StringExtras.h"
2828 #include "llvm/MC/MCContext.h"
2929 #include "llvm/MC/MCInst.h"
30 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionMachO.h"
3131 #include "llvm/MC/MCStreamer.h"
3232 #include "llvm/CodeGen/DwarfWriter.h"
3333 #include "llvm/CodeGen/MachineJumpTableInfo.h"
1616 #include "llvm/ADT/Twine.h"
1717 #include "llvm/MC/MCContext.h"
1818 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSectionMachO.h"
2020 #include "llvm/MC/MCStreamer.h"
2121 #include "llvm/MC/MCSymbol.h"
2222 #include "llvm/Support/SourceMgr.h"
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCSectionMachO.h"
1616 #include "llvm/MC/MCStreamer.h"
1717 #include "llvm/ADT/OwningPtr.h"
1818 #include "llvm/Support/CommandLine.h"