LLVM API Documentation

MCSectionMachO.cpp
Go to the documentation of this file.
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 #include <cctype>
00014 using namespace llvm;
00015 
00016 /// SectionTypeDescriptors - These are strings that describe the various section
00017 /// types.  This *must* be kept in order with and stay synchronized with the
00018 /// section type list.
00019 static const struct {
00020   const char *AssemblerName, *EnumName;
00021 } SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1] = {
00022   { "regular",                  "S_REGULAR" },                    // 0x00
00023   { nullptr,                    "S_ZEROFILL" },                   // 0x01
00024   { "cstring_literals",         "S_CSTRING_LITERALS" },           // 0x02
00025   { "4byte_literals",           "S_4BYTE_LITERALS" },             // 0x03
00026   { "8byte_literals",           "S_8BYTE_LITERALS" },             // 0x04
00027   { "literal_pointers",         "S_LITERAL_POINTERS" },           // 0x05
00028   { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" },   // 0x06
00029   { "lazy_symbol_pointers",     "S_LAZY_SYMBOL_POINTERS" },       // 0x07
00030   { "symbol_stubs",             "S_SYMBOL_STUBS" },               // 0x08
00031   { "mod_init_funcs",           "S_MOD_INIT_FUNC_POINTERS" },     // 0x09
00032   { "mod_term_funcs",           "S_MOD_TERM_FUNC_POINTERS" },     // 0x0A
00033   { "coalesced",                "S_COALESCED" },                  // 0x0B
00034   { nullptr, /*FIXME??*/        "S_GB_ZEROFILL" },                // 0x0C
00035   { "interposing",              "S_INTERPOSING" },                // 0x0D
00036   { "16byte_literals",          "S_16BYTE_LITERALS" },            // 0x0E
00037   { nullptr, /*FIXME??*/        "S_DTRACE_DOF" },                 // 0x0F
00038   { nullptr, /*FIXME??*/        "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
00039   { "thread_local_regular",     "S_THREAD_LOCAL_REGULAR" },       // 0x11
00040   { "thread_local_zerofill",    "S_THREAD_LOCAL_ZEROFILL" },      // 0x12
00041   { "thread_local_variables",   "S_THREAD_LOCAL_VARIABLES" },     // 0x13
00042   { "thread_local_variable_pointers",
00043     "S_THREAD_LOCAL_VARIABLE_POINTERS" },                         // 0x14
00044   { "thread_local_init_function_pointers",
00045     "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"},                     // 0x15
00046 };
00047 
00048 
00049 /// SectionAttrDescriptors - This is an array of descriptors for section
00050 /// attributes.  Unlike the SectionTypeDescriptors, this is not directly indexed
00051 /// by attribute, instead it is searched.
00052 static const struct {
00053   unsigned AttrFlag;
00054   const char *AssemblerName, *EnumName;
00055 } SectionAttrDescriptors[] = {
00056 #define ENTRY(ASMNAME, ENUM) \
00057   { MachO::ENUM, ASMNAME, #ENUM },
00058 ENTRY("pure_instructions",   S_ATTR_PURE_INSTRUCTIONS)
00059 ENTRY("no_toc",              S_ATTR_NO_TOC)
00060 ENTRY("strip_static_syms",   S_ATTR_STRIP_STATIC_SYMS)
00061 ENTRY("no_dead_strip",       S_ATTR_NO_DEAD_STRIP)
00062 ENTRY("live_support",        S_ATTR_LIVE_SUPPORT)
00063 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
00064 ENTRY("debug",               S_ATTR_DEBUG)
00065 ENTRY(nullptr /*FIXME*/,     S_ATTR_SOME_INSTRUCTIONS)
00066 ENTRY(nullptr /*FIXME*/,     S_ATTR_EXT_RELOC)
00067 ENTRY(nullptr /*FIXME*/,     S_ATTR_LOC_RELOC)
00068 #undef ENTRY
00069   { 0, "none", nullptr }, // used if section has no attributes but has a stub size
00070 };
00071 
00072 MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
00073                                unsigned TAA, unsigned reserved2, SectionKind K)
00074   : MCSection(SV_MachO, K), TypeAndAttributes(TAA), Reserved2(reserved2) {
00075   assert(Segment.size() <= 16 && Section.size() <= 16 &&
00076          "Segment or section string too long");
00077   for (unsigned i = 0; i != 16; ++i) {
00078     if (i < Segment.size())
00079       SegmentName[i] = Segment[i];
00080     else
00081       SegmentName[i] = 0;
00082 
00083     if (i < Section.size())
00084       SectionName[i] = Section[i];
00085     else
00086       SectionName[i] = 0;
00087   }
00088 }
00089 
00090 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
00091                                           raw_ostream &OS,
00092                                           const MCExpr *Subsection) const {
00093   OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
00094 
00095   // Get the section type and attributes.
00096   unsigned TAA = getTypeAndAttributes();
00097   if (TAA == 0) {
00098     OS << '\n';
00099     return;
00100   }
00101 
00102   MachO::SectionType SectionType = getType();
00103   assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
00104          "Invalid SectionType specified!");
00105 
00106   if (SectionTypeDescriptors[SectionType].AssemblerName) {
00107     OS << ',';
00108     OS << SectionTypeDescriptors[SectionType].AssemblerName;
00109   } else {
00110     // If we have no name for the attribute, stop here.
00111     OS << '\n';
00112     return;
00113   }
00114 
00115   // If we don't have any attributes, we're done.
00116   unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
00117   if (SectionAttrs == 0) {
00118     // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
00119     // the attribute specifier.
00120     if (Reserved2 != 0)
00121       OS << ",none," << Reserved2;
00122     OS << '\n';
00123     return;
00124   }
00125 
00126   // Check each attribute to see if we have it.
00127   char Separator = ',';
00128   for (unsigned i = 0;
00129        SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
00130        ++i) {
00131     // Check to see if we have this attribute.
00132     if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
00133       continue;
00134 
00135     // Yep, clear it and print it.
00136     SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
00137 
00138     OS << Separator;
00139     if (SectionAttrDescriptors[i].AssemblerName)
00140       OS << SectionAttrDescriptors[i].AssemblerName;
00141     else
00142       OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
00143     Separator = '+';
00144   }
00145 
00146   assert(SectionAttrs == 0 && "Unknown section attributes!");
00147 
00148   // If we have a S_SYMBOL_STUBS size specified, print it.
00149   if (Reserved2 != 0)
00150     OS << ',' << Reserved2;
00151   OS << '\n';
00152 }
00153 
00154 bool MCSectionMachO::UseCodeAlign() const {
00155   return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
00156 }
00157 
00158 bool MCSectionMachO::isVirtualSection() const {
00159   return (getType() == MachO::S_ZEROFILL ||
00160           getType() == MachO::S_GB_ZEROFILL ||
00161           getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
00162 }
00163 
00164 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
00165 /// This is a string that can appear after a .section directive in a mach-o
00166 /// flavored .s file.  If successful, this fills in the specified Out
00167 /// parameters and returns an empty string.  When an invalid section
00168 /// specifier is present, this returns a string indicating the problem.
00169 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec,        // In.
00170                                                   StringRef &Segment,    // Out.
00171                                                   StringRef &Section,    // Out.
00172                                                   unsigned  &TAA,        // Out.
00173                                                   bool      &TAAParsed,  // Out.
00174                                                   unsigned  &StubSize) { // Out.
00175   TAAParsed = false;
00176 
00177   SmallVector<StringRef, 5> SplitSpec;
00178   Spec.split(SplitSpec, ",");
00179   // Remove leading and trailing whitespace.
00180   auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
00181     return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
00182   };
00183   Segment = GetEmptyOrTrim(0);
00184   Section = GetEmptyOrTrim(1);
00185   StringRef SectionType = GetEmptyOrTrim(2);
00186   StringRef Attrs = GetEmptyOrTrim(3);
00187   StringRef StubSizeStr = GetEmptyOrTrim(4);
00188 
00189   // Verify that the segment is present and not too long.
00190   if (Segment.empty() || Segment.size() > 16)
00191     return "mach-o section specifier requires a segment whose length is "
00192            "between 1 and 16 characters";
00193 
00194   // Verify that the section is present and not too long.
00195   if (Section.empty())
00196     return "mach-o section specifier requires a segment and section "
00197            "separated by a comma";
00198 
00199   if (Section.size() > 16)
00200     return "mach-o section specifier requires a section whose length is "
00201            "between 1 and 16 characters";
00202 
00203   // If there is no comma after the section, we're done.
00204   TAA = 0;
00205   StubSize = 0;
00206   if (SectionType.empty())
00207     return "";
00208 
00209   // Figure out which section type it is.
00210   auto TypeDescriptor = std::find_if(
00211       std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors),
00212       [&](decltype(*SectionTypeDescriptors) &Descriptor) {
00213         return Descriptor.AssemblerName &&
00214                SectionType == Descriptor.AssemblerName;
00215       });
00216 
00217   // If we didn't find the section type, reject it.
00218   if (TypeDescriptor == std::end(SectionTypeDescriptors))
00219     return "mach-o section specifier uses an unknown section type";
00220 
00221   // Remember the TypeID.
00222   TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
00223   TAAParsed = true;
00224 
00225   // If we have no comma after the section type, there are no attributes.
00226   if (Attrs.empty()) {
00227     // S_SYMBOL_STUBS always require a symbol stub size specifier.
00228     if (TAA == MachO::S_SYMBOL_STUBS)
00229       return "mach-o section specifier of type 'symbol_stubs' requires a size "
00230              "specifier";
00231     return "";
00232   }
00233 
00234   // The attribute list is a '+' separated list of attributes.
00235   SmallVector<StringRef, 1> SectionAttrs;
00236   Attrs.split(SectionAttrs, "+", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
00237 
00238   for (StringRef &SectionAttr : SectionAttrs) {
00239     auto AttrDescriptorI = std::find_if(
00240         std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors),
00241         [&](decltype(*SectionAttrDescriptors) &Descriptor) {
00242           return Descriptor.AssemblerName &&
00243                  SectionAttr.trim() == Descriptor.AssemblerName;
00244         });
00245     if (AttrDescriptorI == std::end(SectionAttrDescriptors))
00246       return "mach-o section specifier has invalid attribute";
00247 
00248     TAA |= AttrDescriptorI->AttrFlag;
00249   }
00250 
00251   // Okay, we've parsed the section attributes, see if we have a stub size spec.
00252   if (StubSizeStr.empty()) {
00253     // S_SYMBOL_STUBS always require a symbol stub size specifier.
00254     if (TAA == MachO::S_SYMBOL_STUBS)
00255       return "mach-o section specifier of type 'symbol_stubs' requires a size "
00256       "specifier";
00257     return "";
00258   }
00259 
00260   // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
00261   if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
00262     return "mach-o section specifier cannot have a stub size specified because "
00263            "it does not have type 'symbol_stubs'";
00264 
00265   // Convert the stub size from a string to an integer.
00266   if (StubSizeStr.getAsInteger(0, StubSize))
00267     return "mach-o section specifier has a malformed stub size";
00268 
00269   return "";
00270 }