Home | History | Annotate | Download | only in MC
      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"
     13 #include <cctype>
     14 using namespace llvm;
     15 
     16 /// SectionTypeDescriptors - These are strings that describe the various section
     17 /// types.  This *must* be kept in order with and stay synchronized with the
     18 /// section type list.
     19 static const struct {
     20   const char *AssemblerName, *EnumName;
     21 } SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
     22   { "regular",                  "S_REGULAR" },                    // 0x00
     23   { 0,                          "S_ZEROFILL" },                   // 0x01
     24   { "cstring_literals",         "S_CSTRING_LITERALS" },           // 0x02
     25   { "4byte_literals",           "S_4BYTE_LITERALS" },             // 0x03
     26   { "8byte_literals",           "S_8BYTE_LITERALS" },             // 0x04
     27   { "literal_pointers",         "S_LITERAL_POINTERS" },           // 0x05
     28   { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" },   // 0x06
     29   { "lazy_symbol_pointers",     "S_LAZY_SYMBOL_POINTERS" },       // 0x07
     30   { "symbol_stubs",             "S_SYMBOL_STUBS" },               // 0x08
     31   { "mod_init_funcs",           "S_MOD_INIT_FUNC_POINTERS" },     // 0x09
     32   { "mod_term_funcs",           "S_MOD_TERM_FUNC_POINTERS" },     // 0x0A
     33   { "coalesced",                "S_COALESCED" },                  // 0x0B
     34   { 0, /*FIXME??*/              "S_GB_ZEROFILL" },                // 0x0C
     35   { "interposing",              "S_INTERPOSING" },                // 0x0D
     36   { "16byte_literals",          "S_16BYTE_LITERALS" },            // 0x0E
     37   { 0, /*FIXME??*/              "S_DTRACE_DOF" },                 // 0x0F
     38   { 0, /*FIXME??*/              "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
     39   { "thread_local_regular",     "S_THREAD_LOCAL_REGULAR" },       // 0x11
     40   { "thread_local_zerofill",    "S_THREAD_LOCAL_ZEROFILL" },      // 0x12
     41   { "thread_local_variables",   "S_THREAD_LOCAL_VARIABLES" },     // 0x13
     42   { "thread_local_variable_pointers",
     43     "S_THREAD_LOCAL_VARIABLE_POINTERS" },                         // 0x14
     44   { "thread_local_init_function_pointers",
     45     "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"},                     // 0x15
     46 };
     47 
     48 
     49 /// SectionAttrDescriptors - This is an array of descriptors for section
     50 /// attributes.  Unlike the SectionTypeDescriptors, this is not directly indexed
     51 /// by attribute, instead it is searched.  The last entry has an AttrFlagEnd
     52 /// AttrFlag value.
     53 static const struct {
     54   unsigned AttrFlag;
     55   const char *AssemblerName, *EnumName;
     56 } SectionAttrDescriptors[] = {
     57 #define ENTRY(ASMNAME, ENUM) \
     58   { MCSectionMachO::ENUM, ASMNAME, #ENUM },
     59 ENTRY("pure_instructions",   S_ATTR_PURE_INSTRUCTIONS)
     60 ENTRY("no_toc",              S_ATTR_NO_TOC)
     61 ENTRY("strip_static_syms",   S_ATTR_STRIP_STATIC_SYMS)
     62 ENTRY("no_dead_strip",       S_ATTR_NO_DEAD_STRIP)
     63 ENTRY("live_support",        S_ATTR_LIVE_SUPPORT)
     64 ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
     65 ENTRY("debug",               S_ATTR_DEBUG)
     66 ENTRY(0 /*FIXME*/,           S_ATTR_SOME_INSTRUCTIONS)
     67 ENTRY(0 /*FIXME*/,           S_ATTR_EXT_RELOC)
     68 ENTRY(0 /*FIXME*/,           S_ATTR_LOC_RELOC)
     69 #undef ENTRY
     70   { 0, "none", 0 }, // used if section has no attributes but has a stub size
     71 #define AttrFlagEnd 0xffffffff // non legal value, multiple attribute bits set
     72   { AttrFlagEnd, 0, 0 }
     73 };
     74 
     75 MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
     76                                unsigned TAA, unsigned reserved2, SectionKind K)
     77   : MCSection(SV_MachO, K), TypeAndAttributes(TAA), Reserved2(reserved2) {
     78   assert(Segment.size() <= 16 && Section.size() <= 16 &&
     79          "Segment or section string too long");
     80   for (unsigned i = 0; i != 16; ++i) {
     81     if (i < Segment.size())
     82       SegmentName[i] = Segment[i];
     83     else
     84       SegmentName[i] = 0;
     85 
     86     if (i < Section.size())
     87       SectionName[i] = Section[i];
     88     else
     89       SectionName[i] = 0;
     90   }
     91 }
     92 
     93 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
     94                                           raw_ostream &OS) const {
     95   OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
     96 
     97   // Get the section type and attributes.
     98   unsigned TAA = getTypeAndAttributes();
     99   if (TAA == 0) {
    100     OS << '\n';
    101     return;
    102   }
    103 
    104   unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
    105   assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
    106          "Invalid SectionType specified!");
    107 
    108   if (SectionTypeDescriptors[SectionType].AssemblerName) {
    109     OS << ',';
    110     OS << SectionTypeDescriptors[SectionType].AssemblerName;
    111   } else {
    112     // If we have no name for the attribute, stop here.
    113     OS << '\n';
    114     return;
    115   }
    116 
    117   // If we don't have any attributes, we're done.
    118   unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
    119   if (SectionAttrs == 0) {
    120     // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
    121     // the attribute specifier.
    122     if (Reserved2 != 0)
    123       OS << ",none," << Reserved2;
    124     OS << '\n';
    125     return;
    126   }
    127 
    128   // Check each attribute to see if we have it.
    129   char Separator = ',';
    130   for (unsigned i = 0;
    131        SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
    132        ++i) {
    133     // Check to see if we have this attribute.
    134     if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
    135       continue;
    136 
    137     // Yep, clear it and print it.
    138     SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
    139 
    140     OS << Separator;
    141     if (SectionAttrDescriptors[i].AssemblerName)
    142       OS << SectionAttrDescriptors[i].AssemblerName;
    143     else
    144       OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
    145     Separator = '+';
    146   }
    147 
    148   assert(SectionAttrs == 0 && "Unknown section attributes!");
    149 
    150   // If we have a S_SYMBOL_STUBS size specified, print it.
    151   if (Reserved2 != 0)
    152     OS << ',' << Reserved2;
    153   OS << '\n';
    154 }
    155 
    156 bool MCSectionMachO::UseCodeAlign() const {
    157   return hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
    158 }
    159 
    160 bool MCSectionMachO::isVirtualSection() const {
    161   return (getType() == MCSectionMachO::S_ZEROFILL ||
    162           getType() == MCSectionMachO::S_GB_ZEROFILL ||
    163           getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
    164 }
    165 
    166 /// StripSpaces - This removes leading and trailing spaces from the StringRef.
    167 static void StripSpaces(StringRef &Str) {
    168   while (!Str.empty() && isspace(Str[0]))
    169     Str = Str.substr(1);
    170   while (!Str.empty() && isspace(Str.back()))
    171     Str = Str.substr(0, Str.size()-1);
    172 }
    173 
    174 /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
    175 /// This is a string that can appear after a .section directive in a mach-o
    176 /// flavored .s file.  If successful, this fills in the specified Out
    177 /// parameters and returns an empty string.  When an invalid section
    178 /// specifier is present, this returns a string indicating the problem.
    179 std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec,        // In.
    180                                                   StringRef &Segment,    // Out.
    181                                                   StringRef &Section,    // Out.
    182                                                   unsigned  &TAA,        // Out.
    183                                                   bool      &TAAParsed,  // Out.
    184                                                   unsigned  &StubSize) { // Out.
    185   TAAParsed = false;
    186   // Find the first comma.
    187   std::pair<StringRef, StringRef> Comma = Spec.split(',');
    188 
    189   // If there is no comma, we fail.
    190   if (Comma.second.empty())
    191     return "mach-o section specifier requires a segment and section "
    192            "separated by a comma";
    193 
    194   // Capture segment, remove leading and trailing whitespace.
    195   Segment = Comma.first;
    196   StripSpaces(Segment);
    197 
    198   // Verify that the segment is present and not too long.
    199   if (Segment.empty() || Segment.size() > 16)
    200     return "mach-o section specifier requires a segment whose length is "
    201            "between 1 and 16 characters";
    202 
    203   // Split the section name off from any attributes if present.
    204   Comma = Comma.second.split(',');
    205 
    206   // Capture section, remove leading and trailing whitespace.
    207   Section = Comma.first;
    208   StripSpaces(Section);
    209 
    210   // Verify that the section is present and not too long.
    211   if (Section.empty() || Section.size() > 16)
    212     return "mach-o section specifier requires a section whose length is "
    213            "between 1 and 16 characters";
    214 
    215   // If there is no comma after the section, we're done.
    216   TAA = 0;
    217   StubSize = 0;
    218   if (Comma.second.empty())
    219     return "";
    220 
    221   // Otherwise, we need to parse the section type and attributes.
    222   Comma = Comma.second.split(',');
    223 
    224   // Get the section type.
    225   StringRef SectionType = Comma.first;
    226   StripSpaces(SectionType);
    227 
    228   // Figure out which section type it is.
    229   unsigned TypeID;
    230   for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
    231     if (SectionTypeDescriptors[TypeID].AssemblerName &&
    232         SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
    233       break;
    234 
    235   // If we didn't find the section type, reject it.
    236   if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
    237     return "mach-o section specifier uses an unknown section type";
    238 
    239   // Remember the TypeID.
    240   TAA = TypeID;
    241   TAAParsed = true;
    242 
    243   // If we have no comma after the section type, there are no attributes.
    244   if (Comma.second.empty()) {
    245     // S_SYMBOL_STUBS always require a symbol stub size specifier.
    246     if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
    247       return "mach-o section specifier of type 'symbol_stubs' requires a size "
    248              "specifier";
    249     return "";
    250   }
    251 
    252   // Otherwise, we do have some attributes.  Split off the size specifier if
    253   // present.
    254   Comma = Comma.second.split(',');
    255   StringRef Attrs = Comma.first;
    256 
    257   // The attribute list is a '+' separated list of attributes.
    258   std::pair<StringRef, StringRef> Plus = Attrs.split('+');
    259 
    260   while (1) {
    261     StringRef Attr = Plus.first;
    262     StripSpaces(Attr);
    263 
    264     // Look up the attribute.
    265     for (unsigned i = 0; ; ++i) {
    266       if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
    267         return "mach-o section specifier has invalid attribute";
    268 
    269       if (SectionAttrDescriptors[i].AssemblerName &&
    270           Attr == SectionAttrDescriptors[i].AssemblerName) {
    271         TAA |= SectionAttrDescriptors[i].AttrFlag;
    272         break;
    273       }
    274     }
    275 
    276     if (Plus.second.empty()) break;
    277     Plus = Plus.second.split('+');
    278   };
    279 
    280   // Okay, we've parsed the section attributes, see if we have a stub size spec.
    281   if (Comma.second.empty()) {
    282     // S_SYMBOL_STUBS always require a symbol stub size specifier.
    283     if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
    284       return "mach-o section specifier of type 'symbol_stubs' requires a size "
    285       "specifier";
    286     return "";
    287   }
    288 
    289   // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
    290   if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
    291     return "mach-o section specifier cannot have a stub size specified because "
    292            "it does not have type 'symbol_stubs'";
    293 
    294   // Okay, if we do, it must be a number.
    295   StringRef StubSizeStr = Comma.second;
    296   StripSpaces(StubSizeStr);
    297 
    298   // Convert the stub size from a string to an integer.
    299   if (StubSizeStr.getAsInteger(0, StubSize))
    300     return "mach-o section specifier has a malformed stub size";
    301 
    302   return "";
    303 }
    304