1 //===- MCSection.h - Machine Code Sections ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MCSection class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSECTION_H
15 #define LLVM_MC_MCSECTION_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/SectionKind.h"
26 /// MCSection - Instances of this class represent a uniqued identifier for a
27 /// section in the current translation unit. The MCContext class uniques and
30 MCSection(const MCSection&); // DO NOT IMPLEMENT
31 void operator=(const MCSection&); // DO NOT IMPLEMENT
33 MCSection(SectionKind K) : Kind(K) {}
38 SectionKind getKind() const { return Kind; }
40 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
41 raw_ostream &OS) const = 0;
45 class MCSectionELF : public MCSection {
48 /// IsDirective - This is true if the section name is a directive, not
49 /// something that should be printed with ".section".
51 /// FIXME: This is a hack. Switch to a semantic view of the section instead
52 /// of a syntactic one.
55 MCSectionELF(const StringRef &Name, bool IsDirective, SectionKind K,
59 static MCSectionELF *Create(const StringRef &Name, bool IsDirective,
60 SectionKind K, MCContext &Ctx);
62 const std::string &getName() const { return Name; }
63 bool isDirective() const { return IsDirective; }
66 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
67 raw_ostream &OS) const;
71 /// MCSectionMachO - This represents a section on a Mach-O system (used by
72 /// Mac OS X). On a Mac system, these are also described in
73 /// /usr/include/mach-o/loader.h.
74 class MCSectionMachO : public MCSection {
75 char SegmentName[16]; // Not necessarily null terminated!
76 char SectionName[16]; // Not necessarily null terminated!
78 /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
79 /// field of a section, drawn from the enums below.
80 unsigned TypeAndAttributes;
82 /// Reserved2 - The 'reserved2' field of a section, used to represent the
83 /// size of stubs, for example.
86 MCSectionMachO(const StringRef &Segment, const StringRef &Section,
87 unsigned TAA, unsigned reserved2, SectionKind K)
88 : MCSection(K), TypeAndAttributes(TAA), Reserved2(reserved2) {
89 assert(Segment.size() <= 16 && Section.size() <= 16 &&
90 "Segment or section string too long");
91 for (unsigned i = 0; i != 16; ++i) {
92 if (i < Segment.size())
93 SegmentName[i] = Segment[i];
97 if (i < Section.size())
98 SectionName[i] = Section[i];
105 static MCSectionMachO *Create(const StringRef &Segment,
106 const StringRef &Section,
107 unsigned TypeAndAttributes,
109 SectionKind K, MCContext &Ctx);
111 /// These are the section type and attributes fields. A MachO section can
112 /// have only one Type, but can have any of the attributes specified.
114 // TypeAndAttributes bitmasks.
115 SECTION_TYPE = 0x000000FFU,
116 SECTION_ATTRIBUTES = 0xFFFFFF00U,
118 // Valid section types.
120 /// S_REGULAR - Regular section.
122 /// S_ZEROFILL - Zero fill on demand section.
124 /// S_CSTRING_LITERALS - Section with literal C strings.
125 S_CSTRING_LITERALS = 0x02U,
126 /// S_4BYTE_LITERALS - Section with 4 byte literals.
127 S_4BYTE_LITERALS = 0x03U,
128 /// S_8BYTE_LITERALS - Section with 8 byte literals.
129 S_8BYTE_LITERALS = 0x04U,
130 /// S_LITERAL_POINTERS - Section with pointers to literals.
131 S_LITERAL_POINTERS = 0x05U,
132 /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
133 S_NON_LAZY_SYMBOL_POINTERS = 0x06U,
134 /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
135 S_LAZY_SYMBOL_POINTERS = 0x07U,
136 /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
137 /// the Reserved2 field.
138 S_SYMBOL_STUBS = 0x08U,
139 /// S_SYMBOL_STUBS - Section with only function pointers for
141 S_MOD_INIT_FUNC_POINTERS = 0x09U,
142 /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
144 S_MOD_TERM_FUNC_POINTERS = 0x0AU,
145 /// S_COALESCED - Section contains symbols that are to be coalesced.
147 /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
149 S_GB_ZEROFILL = 0x0CU,
150 /// S_INTERPOSING - Section with only pairs of function pointers for
152 S_INTERPOSING = 0x0DU,
153 /// S_16BYTE_LITERALS - Section with only 16 byte literals.
154 S_16BYTE_LITERALS = 0x0EU,
155 /// S_DTRACE_DOF - Section contains DTrace Object Format.
156 S_DTRACE_DOF = 0x0FU,
157 /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
158 /// lazy loaded dylibs.
159 S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
161 LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS,
164 // Valid section attributes.
166 /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
168 S_ATTR_PURE_INSTRUCTIONS = 1U << 31,
169 /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
170 /// in a ranlib table of contents.
171 S_ATTR_NO_TOC = 1U << 30,
172 /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
173 /// in files with the MY_DYLDLINK flag.
174 S_ATTR_STRIP_STATIC_SYMS = 1U << 29,
175 /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
176 S_ATTR_NO_DEAD_STRIP = 1U << 28,
177 /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
178 S_ATTR_LIVE_SUPPORT = 1U << 27,
179 /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
181 S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
182 /// S_ATTR_DEBUG - A debug section.
183 S_ATTR_DEBUG = 1U << 25,
184 /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
185 S_ATTR_SOME_INSTRUCTIONS = 1U << 10,
186 /// S_ATTR_EXT_RELOC - Section has external relocation entries.
187 S_ATTR_EXT_RELOC = 1U << 9,
188 /// S_ATTR_LOC_RELOC - Section has local relocation entries.
189 S_ATTR_LOC_RELOC = 1U << 8
192 StringRef getSegmentName() const {
193 // SegmentName is not necessarily null terminated!
195 return StringRef(SegmentName, 16);
196 return StringRef(SegmentName);
198 StringRef getSectionName() const {
199 // SectionName is not necessarily null terminated!
201 return StringRef(SectionName, 16);
202 return StringRef(SectionName);
205 unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
208 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
209 /// This is a string that can appear after a .section directive in a mach-o
210 /// flavored .s file. If successful, this fills in the specified Out
211 /// parameters and returns an empty string. When an invalid section
212 /// specifier is present, this returns a string indicating the problem.
213 static std::string ParseSectionSpecifier(StringRef Spec, // In.
214 StringRef &Segment, // Out.
215 StringRef &Section, // Out.
216 unsigned &TAA, // Out.
217 unsigned &StubSize); // Out.
219 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
220 raw_ostream &OS) const;
223 class MCSectionCOFF : public MCSection {
226 /// IsDirective - This is true if the section name is a directive, not
227 /// something that should be printed with ".section".
229 /// FIXME: This is a hack. Switch to a semantic view of the section instead
230 /// of a syntactic one.
233 MCSectionCOFF(const StringRef &Name, bool IsDirective, SectionKind K,
237 static MCSectionCOFF *Create(const StringRef &Name, bool IsDirective,
238 SectionKind K, MCContext &Ctx);
240 const std::string &getName() const { return Name; }
241 bool isDirective() const { return IsDirective; }
243 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
244 raw_ostream &OS) const;
247 } // end namespace llvm