MCSection.h revision ff4bc460c52c1f285d8a56da173641bf92d49e3f
1//===- MCSection.h - Machine Code Sections ----------------------*- C++ -*-===// 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// This file declares the MCSection class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_MC_MCSECTION_H 15#define LLVM_MC_MCSECTION_H 16 17#include <string> 18#include "llvm/ADT/StringRef.h" 19#include "llvm/MC/SectionKind.h" 20 21namespace llvm { 22 class MCContext; 23 class TargetAsmInfo; 24 class raw_ostream; 25 26 /// MCSection - Instances of this class represent a uniqued identifier for a 27 /// section in the current translation unit. The MCContext class uniques and 28 /// creates these. 29 class MCSection { 30 MCSection(const MCSection&); // DO NOT IMPLEMENT 31 void operator=(const MCSection&); // DO NOT IMPLEMENT 32 protected: 33 MCSection(SectionKind K) : Kind(K) {} 34 SectionKind Kind; 35 public: 36 virtual ~MCSection(); 37 38 SectionKind getKind() const { return Kind; } 39 40 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI, 41 raw_ostream &OS) const = 0; 42 }; 43 44 45 class MCSectionELF : public MCSection { 46 std::string Name; 47 48 /// IsDirective - This is true if the section name is a directive, not 49 /// something that should be printed with ".section". 50 /// 51 /// FIXME: This is a hack. Switch to a semantic view of the section instead 52 /// of a syntactic one. 53 bool IsDirective; 54 55 MCSectionELF(const StringRef &Name, bool IsDirective, SectionKind K, 56 MCContext &Ctx); 57 public: 58 59 static MCSectionELF *Create(const StringRef &Name, bool IsDirective, 60 SectionKind K, MCContext &Ctx); 61 62 const std::string &getName() const { return Name; } 63 bool isDirective() const { return IsDirective; } 64 65 66 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI, 67 raw_ostream &OS) const; 68 }; 69 70 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! 77 78 /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES 79 /// field of a section, drawn from the enums below. 80 unsigned TypeAndAttributes; 81 82 /// Reserved2 - The 'reserved2' field of a section, used to represent the 83 /// size of stubs, for example. 84 unsigned Reserved2; 85 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]; 94 else 95 SegmentName[i] = 0; 96 97 if (i < Section.size()) 98 SectionName[i] = Section[i]; 99 else 100 SectionName[i] = 0; 101 } 102 } 103 public: 104 105 static MCSectionMachO *Create(const StringRef &Segment, 106 const StringRef &Section, 107 unsigned TypeAndAttributes, 108 unsigned Reserved2, 109 SectionKind K, MCContext &Ctx); 110 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. 113 enum { 114 // TypeAndAttributes bitmasks. 115 SECTION_TYPE = 0x000000FFU, 116 SECTION_ATTRIBUTES = 0xFFFFFF00U, 117 118 // Valid section types. 119 120 /// S_REGULAR - Regular section. 121 S_REGULAR = 0x00U, 122 /// S_ZEROFILL - Zero fill on demand section. 123 S_ZEROFILL = 0x01U, 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 140 /// initialization. 141 S_MOD_INIT_FUNC_POINTERS = 0x09U, 142 /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for 143 /// termination. 144 S_MOD_TERM_FUNC_POINTERS = 0x0AU, 145 /// S_COALESCED - Section contains symbols that are to be coalesced. 146 S_COALESCED = 0x0BU, 147 /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 148 /// gigabytes). 149 S_GB_ZEROFILL = 0x0CU, 150 /// S_INTERPOSING - Section with only pairs of function pointers for 151 /// interposing. 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, 160 161 LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS, 162 163 164 // Valid section attributes. 165 166 /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine 167 /// instructions. 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 180 /// dyld. 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 190 }; 191 192 StringRef getSegmentName() const { 193 // SegmentName is not necessarily null terminated! 194 if (SegmentName[15]) 195 return StringRef(SegmentName, 16); 196 return StringRef(SegmentName); 197 } 198 StringRef getSectionName() const { 199 // SectionName is not necessarily null terminated! 200 if (SectionName[15]) 201 return StringRef(SectionName, 16); 202 return StringRef(SectionName); 203 } 204 205 unsigned getTypeAndAttributes() const { return TypeAndAttributes; } 206 207 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. 218 219 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI, 220 raw_ostream &OS) const; 221 }; 222 223 class MCSectionCOFF : public MCSection { 224 std::string Name; 225 226 /// IsDirective - This is true if the section name is a directive, not 227 /// something that should be printed with ".section". 228 /// 229 /// FIXME: This is a hack. Switch to a semantic view of the section instead 230 /// of a syntactic one. 231 bool IsDirective; 232 233 MCSectionCOFF(const StringRef &Name, bool IsDirective, SectionKind K, 234 MCContext &Ctx); 235 public: 236 237 static MCSectionCOFF *Create(const StringRef &Name, bool IsDirective, 238 SectionKind K, MCContext &Ctx); 239 240 const std::string &getName() const { return Name; } 241 bool isDirective() const { return IsDirective; } 242 243 virtual void PrintSwitchToSection(const TargetAsmInfo &TAI, 244 raw_ostream &OS) const; 245 }; 246 247} // end namespace llvm 248 249#endif 250