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