LLVM API Documentation
00001 //===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/MC/MCSectionMachO.h" 00011 #include "llvm/MC/MCContext.h" 00012 #include "llvm/Support/raw_ostream.h" 00013 using namespace llvm; 00014 00015 /// SectionTypeDescriptors - These are strings that describe the various section 00016 /// types. This *must* be kept in order with and stay synchronized with the 00017 /// section type list. 00018 static const struct { 00019 const char *AssemblerName, *EnumName; 00020 } SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = { 00021 { "regular", "S_REGULAR" }, // 0x00 00022 { 0, "S_ZEROFILL" }, // 0x01 00023 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02 00024 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03 00025 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04 00026 { "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05 00027 { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06 00028 { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07 00029 { "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08 00030 { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09 00031 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A 00032 { "coalesced", "S_COALESCED" }, // 0x0B 00033 { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C 00034 { "interposing", "S_INTERPOSING" }, // 0x0D 00035 { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E 00036 { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F 00037 { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" } // 0x10 00038 }; 00039 00040 00041 /// SectionAttrDescriptors - This is an array of descriptors for section 00042 /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed 00043 /// by attribute, instead it is searched. The last entry has a zero AttrFlag 00044 /// value. 00045 static const struct { 00046 unsigned AttrFlag; 00047 const char *AssemblerName, *EnumName; 00048 } SectionAttrDescriptors[] = { 00049 #define ENTRY(ASMNAME, ENUM) \ 00050 { MCSectionMachO::ENUM, ASMNAME, #ENUM }, 00051 ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS) 00052 ENTRY("no_toc", S_ATTR_NO_TOC) 00053 ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS) 00054 ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP) 00055 ENTRY("live_support", S_ATTR_LIVE_SUPPORT) 00056 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE) 00057 ENTRY("debug", S_ATTR_DEBUG) 00058 ENTRY(0 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS) 00059 ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC) 00060 ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC) 00061 #undef ENTRY 00062 { 0, "none", 0 } 00063 }; 00064 00065 00066 MCSectionMachO *MCSectionMachO:: 00067 Create(const StringRef &Segment, const StringRef &Section, 00068 unsigned TypeAndAttributes, unsigned Reserved2, 00069 SectionKind K, MCContext &Ctx) { 00070 // S_SYMBOL_STUBS must be set for Reserved2 to be non-zero. 00071 return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes, 00072 Reserved2, K); 00073 } 00074 00075 void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI, 00076 raw_ostream &OS) const { 00077 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName(); 00078 00079 // Get the section type and attributes. 00080 unsigned TAA = getTypeAndAttributes(); 00081 if (TAA == 0) { 00082 OS << '\n'; 00083 return; 00084 } 00085 00086 OS << ','; 00087 00088 unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE; 00089 assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE && 00090 "Invalid SectionType specified!"); 00091 00092 if (SectionTypeDescriptors[SectionType].AssemblerName) 00093 OS << SectionTypeDescriptors[SectionType].AssemblerName; 00094 else 00095 OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>"; 00096 00097 // If we don't have any attributes, we're done. 00098 unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES; 00099 if (SectionAttrs == 0) { 00100 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as 00101 // the attribute specifier. 00102 if (Reserved2 != 0) 00103 OS << ",none," << Reserved2; 00104 OS << '\n'; 00105 return; 00106 } 00107 00108 // Check each attribute to see if we have it. 00109 char Separator = ','; 00110 for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) { 00111 // Check to see if we have this attribute. 00112 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0) 00113 continue; 00114 00115 // Yep, clear it and print it. 00116 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag; 00117 00118 OS << Separator; 00119 if (SectionAttrDescriptors[i].AssemblerName) 00120 OS << SectionAttrDescriptors[i].AssemblerName; 00121 else 00122 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>"; 00123 Separator = '+'; 00124 } 00125 00126 assert(SectionAttrs == 0 && "Unknown section attributes!"); 00127 00128 // If we have a S_SYMBOL_STUBS size specified, print it. 00129 if (Reserved2 != 0) 00130 OS << ',' << Reserved2; 00131 OS << '\n'; 00132 } 00133 00134 /// StripSpaces - This removes leading and trailing spaces from the StringRef. 00135 static void StripSpaces(StringRef &Str) { 00136 while (!Str.empty() && isspace(Str[0])) 00137 Str = Str.substr(1); 00138 while (!Str.empty() && isspace(Str.back())) 00139 Str = Str.substr(0, Str.size()-1); 00140 } 00141 00142 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec". 00143 /// This is a string that can appear after a .section directive in a mach-o 00144 /// flavored .s file. If successful, this fills in the specified Out 00145 /// parameters and returns an empty string. When an invalid section 00146 /// specifier is present, this returns a string indicating the problem. 00147 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. 00148 StringRef &Segment, // Out. 00149 StringRef &Section, // Out. 00150 unsigned &TAA, // Out. 00151 unsigned &StubSize) { // Out. 00152 // Find the first comma. 00153 std::pair<StringRef, StringRef> Comma = Spec.split(','); 00154 00155 // If there is no comma, we fail. 00156 if (Comma.second.empty()) 00157 return "mach-o section specifier requires a segment and section " 00158 "separated by a comma"; 00159 00160 // Capture segment, remove leading and trailing whitespace. 00161 Segment = Comma.first; 00162 StripSpaces(Segment); 00163 00164 // Verify that the segment is present and not too long. 00165 if (Segment.empty() || Segment.size() > 16) 00166 return "mach-o section specifier requires a segment whose length is " 00167 "between 1 and 16 characters"; 00168 00169 // Split the section name off from any attributes if present. 00170 Comma = Comma.second.split(','); 00171 00172 // Capture section, remove leading and trailing whitespace. 00173 Section = Comma.first; 00174 StripSpaces(Section); 00175 00176 // Verify that the section is present and not too long. 00177 if (Section.empty() || Section.size() > 16) 00178 return "mach-o section specifier requires a section whose length is " 00179 "between 1 and 16 characters"; 00180 00181 // If there is no comma after the section, we're done. 00182 TAA = 0; 00183 StubSize = 0; 00184 if (Comma.second.empty()) 00185 return ""; 00186 00187 // Otherwise, we need to parse the section type and attributes. 00188 Comma = Comma.second.split(','); 00189 00190 // Get the section type. 00191 StringRef SectionType = Comma.first; 00192 StripSpaces(SectionType); 00193 00194 // Figure out which section type it is. 00195 unsigned TypeID; 00196 for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID) 00197 if (SectionTypeDescriptors[TypeID].AssemblerName && 00198 SectionType == SectionTypeDescriptors[TypeID].AssemblerName) 00199 break; 00200 00201 // If we didn't find the section type, reject it. 00202 if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE) 00203 return "mach-o section specifier uses an unknown section type"; 00204 00205 // Remember the TypeID. 00206 TAA = TypeID; 00207 00208 // If we have no comma after the section type, there are no attributes. 00209 if (Comma.second.empty()) { 00210 // S_SYMBOL_STUBS always require a symbol stub size specifier. 00211 if (TAA == MCSectionMachO::S_SYMBOL_STUBS) 00212 return "mach-o section specifier of type 'symbol_stubs' requires a size " 00213 "specifier"; 00214 return ""; 00215 } 00216 00217 // Otherwise, we do have some attributes. Split off the size specifier if 00218 // present. 00219 Comma = Comma.second.split(','); 00220 StringRef Attrs = Comma.first; 00221 00222 // The attribute list is a '+' separated list of attributes. 00223 std::pair<StringRef, StringRef> Plus = Attrs.split('+'); 00224 00225 while (1) { 00226 StringRef Attr = Plus.first; 00227 StripSpaces(Attr); 00228 00229 // Look up the attribute. 00230 for (unsigned i = 0; ; ++i) { 00231 if (SectionAttrDescriptors[i].AttrFlag == 0) 00232 return "mach-o section specifier has invalid attribute"; 00233 00234 if (SectionAttrDescriptors[i].AssemblerName && 00235 Attr == SectionAttrDescriptors[i].AssemblerName) { 00236 TAA |= SectionAttrDescriptors[i].AttrFlag; 00237 break; 00238 } 00239 } 00240 00241 if (Plus.second.empty()) break; 00242 Plus = Plus.second.split('+'); 00243 }; 00244 00245 // Okay, we've parsed the section attributes, see if we have a stub size spec. 00246 if (Comma.second.empty()) { 00247 // S_SYMBOL_STUBS always require a symbol stub size specifier. 00248 if (TAA == MCSectionMachO::S_SYMBOL_STUBS) 00249 return "mach-o section specifier of type 'symbol_stubs' requires a size " 00250 "specifier"; 00251 return ""; 00252 } 00253 00254 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS. 00255 if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS) 00256 return "mach-o section specifier cannot have a stub size specified because " 00257 "it does not have type 'symbol_stubs'"; 00258 00259 // Okay, if we do, it must be a number. 00260 StringRef StubSizeStr = Comma.second; 00261 StripSpaces(StubSizeStr); 00262 00263 // Convert the a null terminated buffer for strtoul. 00264 char TmpBuffer[32]; 00265 if (StubSizeStr.size() >= 32) 00266 return"mach-o section specifier has a stub size specifier that is too long"; 00267 00268 memcpy(TmpBuffer, StubSizeStr.data(), StubSizeStr.size()); 00269 TmpBuffer[StubSizeStr.size()] = 0; 00270 00271 char *EndPtr; 00272 StubSize = strtoul(TmpBuffer, &EndPtr, 0); 00273 00274 if (EndPtr[0] != 0) 00275 return "mach-o section specifier has a malformed stub size"; 00276 00277 return ""; 00278 } 00279