MCSectionMachO.h revision af76e592c7f9deff0e55c13dbb4a34f07f1c7f64
1b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//===- MCSectionMachO.h - MachO Machine Code Sections -----------*- C++ -*-===//
2b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//
3b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//                     The LLVM Compiler Infrastructure
4b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//
5b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock// This file is distributed under the University of Illinois Open Source
6b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock// License. See LICENSE.TXT for details.
7b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//
8b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//===----------------------------------------------------------------------===//
9b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//
10b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock// This file declares the MCSectionMachO class.
11b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//
12b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock//===----------------------------------------------------------------------===//
13b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
14b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock#ifndef LLVM_MC_MCSECTIONMACHO_H
15b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock#define LLVM_MC_MCSECTIONMACHO_H
16b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
17b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock#include "llvm/MC/MCSection.h"
18b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
197340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlocknamespace llvm {
20b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
21b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock/// MCSectionMachO - This represents a section on a Mach-O system (used by
22b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock/// Mac OS X).  On a Mac system, these are also described in
23b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock/// /usr/include/mach-o/loader.h.
24b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlockclass MCSectionMachO : public MCSection {
25b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  char SegmentName[16];  // Not necessarily null terminated!
26b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  char SectionName[16];  // Not necessarily null terminated!
27b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
287340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
29b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  /// field of a section, drawn from the enums below.
30b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  unsigned TypeAndAttributes;
317340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
32b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  /// Reserved2 - The 'reserved2' field of a section, used to represent the
33b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  /// size of stubs, for example.
34b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  unsigned Reserved2;
35b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
36b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  MCSectionMachO(const StringRef &Segment, const StringRef &Section,
377340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                 unsigned TAA, unsigned reserved2, SectionKind K)
38b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    : MCSection(K), TypeAndAttributes(TAA), Reserved2(reserved2) {
39b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    assert(Segment.size() <= 16 && Section.size() <= 16 &&
407340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock           "Segment or section string too long");
41b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    for (unsigned i = 0; i != 16; ++i) {
42b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock      if (i < Segment.size())
43b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock        SegmentName[i] = Segment[i];
444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock      else
45b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock        SegmentName[i] = 0;
46b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
47b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock      if (i < Section.size())
4825e2d24f24040e58a61169db797f760b16c11f5dJohn Spurlock        SectionName[i] = Section[i];
4925e2d24f24040e58a61169db797f760b16c11f5dJohn Spurlock      else
50b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock        SectionName[i] = 0;
51b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    }
52e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock  }
53b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlockpublic:
54b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
55b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  static MCSectionMachO *Create(const StringRef &Segment,
567340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                                const StringRef &Section,
577340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                                unsigned TypeAndAttributes,
587340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                                unsigned Reserved2,
597340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                                SectionKind K, MCContext &Ctx);
607340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
617340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// These are the section type and attributes fields.  A MachO section can
627340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// have only one Type, but can have any of the attributes specified.
637340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  enum {
647340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    // TypeAndAttributes bitmasks.
657340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    SECTION_TYPE       = 0x000000FFU,
667340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    SECTION_ATTRIBUTES = 0xFFFFFF00U,
677340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // Valid section types.
69b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
707340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_REGULAR - Regular section.
71b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_REGULAR                    = 0x00U,
72af8d6c44f06d2f8baac2c5774a9efdae3fc36797John Spurlock    /// S_ZEROFILL - Zero fill on demand section.
73e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    S_ZEROFILL                   = 0x01U,
74b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_CSTRING_LITERALS - Section with literal C strings.
75b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_CSTRING_LITERALS           = 0x02U,
767340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_4BYTE_LITERALS - Section with 4 byte literals.
77b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_4BYTE_LITERALS             = 0x03U,
787340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_8BYTE_LITERALS - Section with 8 byte literals.
797340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_8BYTE_LITERALS             = 0x04U,
807340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_LITERAL_POINTERS - Section with pointers to literals.
817340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_LITERAL_POINTERS           = 0x05U,
82b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
837340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_NON_LAZY_SYMBOL_POINTERS   = 0x06U,
84b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
857340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_LAZY_SYMBOL_POINTERS       = 0x07U,
86b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
877340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// the Reserved2 field.
887340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_SYMBOL_STUBS               = 0x08U,
89b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_SYMBOL_STUBS - Section with only function pointers for
90b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer    /// initialization.
91b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer    S_MOD_INIT_FUNC_POINTERS     = 0x09U,
92b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer    /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
93b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer    /// termination.
947340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_MOD_TERM_FUNC_POINTERS     = 0x0AU,
95b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_COALESCED - Section contains symbols that are to be coalesced.
96b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_COALESCED                  = 0x0BU,
97b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
98b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// gigabytes).
997340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_GB_ZEROFILL                = 0x0CU,
1007340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_INTERPOSING - Section with only pairs of function pointers for
101b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// interposing.
102b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_INTERPOSING                = 0x0DU,
1037340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_16BYTE_LITERALS - Section with only 16 byte literals.
1047340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_16BYTE_LITERALS            = 0x0EU,
1057340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_DTRACE_DOF - Section contains DTrace Object Format.
1067340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_DTRACE_DOF                 = 0x0FU,
107b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
108b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// lazy loaded dylibs.
1097340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
1107340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1113b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS,
1127340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
113e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
114e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    // Valid section attributes.
1157340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1167340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
1177340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// instructions.
1187340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_ATTR_PURE_INSTRUCTIONS   = 1U << 31,
1197340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
1207340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// in a ranlib table of contents.
1217340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_ATTR_NO_TOC              = 1U << 30,
1227340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
1237340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// in files with the MY_DYLDLINK flag.
124b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_ATTR_STRIP_STATIC_SYMS   = 1U << 29,
125b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
12625e2d24f24040e58a61169db797f760b16c11f5dJohn Spurlock    S_ATTR_NO_DEAD_STRIP       = 1U << 28,
127e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
128b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_ATTR_LIVE_SUPPORT        = 1U << 27,
1297340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
13025e2d24f24040e58a61169db797f760b16c11f5dJohn Spurlock    /// dyld.
131e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
132b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    /// S_ATTR_DEBUG - A debug section.
133b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_ATTR_DEBUG               = 1U << 25,
134e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
1357340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    S_ATTR_SOME_INSTRUCTIONS   = 1U << 10,
13625e2d24f24040e58a61169db797f760b16c11f5dJohn Spurlock    /// S_ATTR_EXT_RELOC - Section has external relocation entries.
137e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    S_ATTR_EXT_RELOC           = 1U << 9,
1387340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    /// S_ATTR_LOC_RELOC - Section has local relocation entries.
139b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    S_ATTR_LOC_RELOC           = 1U << 8
140b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  };
141b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
142b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  StringRef getSegmentName() const {
143b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    // SegmentName is not necessarily null terminated!
144e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    if (SegmentName[15])
145e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock      return StringRef(SegmentName, 16);
146e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    return StringRef(SegmentName);
1477340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  }
148b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  StringRef getSectionName() const {
149b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    // SectionName is not necessarily null terminated!
1507340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    if (SectionName[15])
1517340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock      return StringRef(SectionName, 16);
152b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock    return StringRef(SectionName);
153b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  }
154b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
155b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
1567340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  unsigned getStubSize() const { return Reserved2; }
157b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock
158b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
1597340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// This is a string that can appear after a .section directive in a mach-o
160b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  /// flavored .s file.  If successful, this fills in the specified Out
1617340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// parameters and returns an empty string.  When an invalid section
1627340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock  /// specifier is present, this returns a string indicating the problem.
163b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock  static std::string ParseSectionSpecifier(StringRef Spec,       // In.
164b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock                                           StringRef &Segment,   // Out.
165b408e8ecd22853faa70e97d0596aac9e7dcf5596John Spurlock                                           StringRef &Section,   // Out.
166b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer                                           unsigned  &TAA,       // Out.
167b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer                                           unsigned  &StubSize); // Out.
168b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer
169b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer  virtual void PrintSwitchToSection(const MCAsmInfo &TAI,
170b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer                                    raw_ostream &OS) const;
171b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer};
172b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer
173b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer} // end namespace llvm
174b53dfd4faeb57a51b6cdfc6bde6a441ef9a7ec31Christoph Studer
1757340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock#endif
1767340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock