Home | History | Annotate | Download | only in MC
      1 //===- MCSectionMachO.h - MachO 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 MCSectionMachO class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_MC_MCSECTIONMACHO_H
     15 #define LLVM_MC_MCSECTIONMACHO_H
     16 
     17 #include "llvm/MC/MCSection.h"
     18 
     19 namespace llvm {
     20 
     21 /// MCSectionMachO - This represents a section on a Mach-O system (used by
     22 /// Mac OS X).  On a Mac system, these are also described in
     23 /// /usr/include/mach-o/loader.h.
     24 class MCSectionMachO : public MCSection {
     25   char SegmentName[16];  // Not necessarily null terminated!
     26   char SectionName[16];  // Not necessarily null terminated!
     27 
     28   /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
     29   /// field of a section, drawn from the enums below.
     30   unsigned TypeAndAttributes;
     31 
     32   /// Reserved2 - The 'reserved2' field of a section, used to represent the
     33   /// size of stubs, for example.
     34   unsigned Reserved2;
     35 
     36   MCSectionMachO(StringRef Segment, StringRef Section,
     37                  unsigned TAA, unsigned reserved2, SectionKind K);
     38   friend class MCContext;
     39 public:
     40 
     41   /// These are the section type and attributes fields.  A MachO section can
     42   /// have only one Type, but can have any of the attributes specified.
     43   enum {
     44     // TypeAndAttributes bitmasks.
     45     SECTION_TYPE       = 0x000000FFU,
     46     SECTION_ATTRIBUTES = 0xFFFFFF00U,
     47 
     48     // Valid section types.
     49 
     50     /// S_REGULAR - Regular section.
     51     S_REGULAR                    = 0x00U,
     52     /// S_ZEROFILL - Zero fill on demand section.
     53     S_ZEROFILL                   = 0x01U,
     54     /// S_CSTRING_LITERALS - Section with literal C strings.
     55     S_CSTRING_LITERALS           = 0x02U,
     56     /// S_4BYTE_LITERALS - Section with 4 byte literals.
     57     S_4BYTE_LITERALS             = 0x03U,
     58     /// S_8BYTE_LITERALS - Section with 8 byte literals.
     59     S_8BYTE_LITERALS             = 0x04U,
     60     /// S_LITERAL_POINTERS - Section with pointers to literals.
     61     S_LITERAL_POINTERS           = 0x05U,
     62     /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
     63     S_NON_LAZY_SYMBOL_POINTERS   = 0x06U,
     64     /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
     65     S_LAZY_SYMBOL_POINTERS       = 0x07U,
     66     /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
     67     /// the Reserved2 field.
     68     S_SYMBOL_STUBS               = 0x08U,
     69     /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
     70     /// initialization.
     71     S_MOD_INIT_FUNC_POINTERS     = 0x09U,
     72     /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
     73     /// termination.
     74     S_MOD_TERM_FUNC_POINTERS     = 0x0AU,
     75     /// S_COALESCED - Section contains symbols that are to be coalesced.
     76     S_COALESCED                  = 0x0BU,
     77     /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
     78     /// gigabytes).
     79     S_GB_ZEROFILL                = 0x0CU,
     80     /// S_INTERPOSING - Section with only pairs of function pointers for
     81     /// interposing.
     82     S_INTERPOSING                = 0x0DU,
     83     /// S_16BYTE_LITERALS - Section with only 16 byte literals.
     84     S_16BYTE_LITERALS            = 0x0EU,
     85     /// S_DTRACE_DOF - Section contains DTrace Object Format.
     86     S_DTRACE_DOF                 = 0x0FU,
     87     /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
     88     /// lazy loaded dylibs.
     89     S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
     90     /// S_THREAD_LOCAL_REGULAR - Section with ....
     91     S_THREAD_LOCAL_REGULAR = 0x11U,
     92     /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
     93     S_THREAD_LOCAL_ZEROFILL = 0x12U,
     94     /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable structure
     95     /// data.
     96     S_THREAD_LOCAL_VARIABLES = 0x13U,
     97     /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with ....
     98     S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14U,
     99     /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
    100     /// variable initialization pointers to functions.
    101     S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15U,
    102 
    103     LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
    104 
    105 
    106     // Valid section attributes.
    107 
    108     /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
    109     /// instructions.
    110     S_ATTR_PURE_INSTRUCTIONS   = 1U << 31,
    111     /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
    112     /// in a ranlib table of contents.
    113     S_ATTR_NO_TOC              = 1U << 30,
    114     /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
    115     /// in files with the MY_DYLDLINK flag.
    116     S_ATTR_STRIP_STATIC_SYMS   = 1U << 29,
    117     /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
    118     S_ATTR_NO_DEAD_STRIP       = 1U << 28,
    119     /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
    120     S_ATTR_LIVE_SUPPORT        = 1U << 27,
    121     /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
    122     /// dyld.
    123     S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
    124     /// S_ATTR_DEBUG - A debug section.
    125     S_ATTR_DEBUG               = 1U << 25,
    126     /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
    127     S_ATTR_SOME_INSTRUCTIONS   = 1U << 10,
    128     /// S_ATTR_EXT_RELOC - Section has external relocation entries.
    129     S_ATTR_EXT_RELOC           = 1U << 9,
    130     /// S_ATTR_LOC_RELOC - Section has local relocation entries.
    131     S_ATTR_LOC_RELOC           = 1U << 8
    132   };
    133 
    134   StringRef getSegmentName() const {
    135     // SegmentName is not necessarily null terminated!
    136     if (SegmentName[15])
    137       return StringRef(SegmentName, 16);
    138     return StringRef(SegmentName);
    139   }
    140   StringRef getSectionName() const {
    141     // SectionName is not necessarily null terminated!
    142     if (SectionName[15])
    143       return StringRef(SectionName, 16);
    144     return StringRef(SectionName);
    145   }
    146 
    147   unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
    148   unsigned getStubSize() const { return Reserved2; }
    149 
    150   unsigned getType() const { return TypeAndAttributes & SECTION_TYPE; }
    151   bool hasAttribute(unsigned Value) const {
    152     return (TypeAndAttributes & Value) != 0;
    153   }
    154 
    155   /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
    156   /// This is a string that can appear after a .section directive in a mach-o
    157   /// flavored .s file.  If successful, this fills in the specified Out
    158   /// parameters and returns an empty string.  When an invalid section
    159   /// specifier is present, this returns a string indicating the problem.
    160   /// If no TAA was parsed, TAA is not altered, and TAAWasSet becomes false.
    161   static std::string ParseSectionSpecifier(StringRef Spec,       // In.
    162                                            StringRef &Segment,   // Out.
    163                                            StringRef &Section,   // Out.
    164                                            unsigned  &TAA,       // Out.
    165                                            bool      &TAAParsed, // Out.
    166                                            unsigned  &StubSize); // Out.
    167 
    168   virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
    169                                     raw_ostream &OS) const;
    170   virtual bool UseCodeAlign() const;
    171   virtual bool isVirtualSection() const;
    172 
    173   static bool classof(const MCSection *S) {
    174     return S->getVariant() == SV_MachO;
    175   }
    176   static bool classof(const MCSectionMachO *) { return true; }
    177 };
    178 
    179 } // end namespace llvm
    180 
    181 #endif
    182