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