xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCSectionMachO.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- MCSectionMachO.h - MachO Machine Code Sections -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the MCSectionMachO class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCSECTIONMACHO_H
14 #define LLVM_MC_MCSECTIONMACHO_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 #include "llvm/MC/MCSection.h"
19 
20 namespace llvm {
21 
22 /// This represents a section on a Mach-O system (used by Mac OS X).  On a Mac
23 /// system, these are also described in /usr/include/mach-o/loader.h.
24 class MCSectionMachO final : public MCSection {
25   char SegmentName[16];  // Not necessarily null terminated!
26 
27   /// This is the SECTION_TYPE and SECTION_ATTRIBUTES field of a section, drawn
28   /// from the enums below.
29   unsigned TypeAndAttributes;
30 
31   /// The 'reserved2' field of a section, used to represent the size of stubs,
32   /// for example.
33   unsigned Reserved2;
34 
35   // The index of this section in MachObjectWriter::SectionOrder, which is
36   // different from MCSection::Ordinal.
37   unsigned LayoutOrder = 0;
38 
39   // The defining non-temporary symbol for each fragment.
40   SmallVector<const MCSymbol *, 0> Atoms;
41 
42   MCSectionMachO(StringRef Segment, StringRef Section, unsigned TAA,
43                  unsigned reserved2, SectionKind K, MCSymbol *Begin);
44   friend class MCContext;
45 public:
46 
getSegmentName()47   StringRef getSegmentName() const {
48     // SegmentName is not necessarily null terminated!
49     if (SegmentName[15])
50       return StringRef(SegmentName, 16);
51     return StringRef(SegmentName);
52   }
53 
getTypeAndAttributes()54   unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
getStubSize()55   unsigned getStubSize() const { return Reserved2; }
56 
getType()57   MachO::SectionType getType() const {
58     return static_cast<MachO::SectionType>(TypeAndAttributes &
59                                            MachO::SECTION_TYPE);
60   }
hasAttribute(unsigned Value)61   bool hasAttribute(unsigned Value) const {
62     return (TypeAndAttributes & Value) != 0;
63   }
64 
65   /// Parse the section specifier indicated by "Spec". This is a string that can
66   /// appear after a .section directive in a mach-o flavored .s file.  If
67   /// successful, this fills in the specified Out parameters and returns an
68   /// empty string.  When an invalid section specifier is present, this returns
69   /// an Error indicating the problem. If no TAA was parsed, TAA is not altered,
70   /// and TAAWasSet becomes false.
71   static Error ParseSectionSpecifier(StringRef Spec,      // In.
72                                      StringRef &Segment,  // Out.
73                                      StringRef &Section,  // Out.
74                                      unsigned &TAA,       // Out.
75                                      bool &TAAParsed,     // Out.
76                                      unsigned &StubSize); // Out.
77 
78   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
79                             raw_ostream &OS,
80                             uint32_t Subsection) const override;
81   bool useCodeAlign() const override;
82 
83   void allocAtoms();
84   const MCSymbol *getAtom(size_t I) const;
85   void setAtom(size_t I, const MCSymbol *Sym);
86 
getLayoutOrder()87   unsigned getLayoutOrder() const { return LayoutOrder; }
setLayoutOrder(unsigned Value)88   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
89 
classof(const MCSection * S)90   static bool classof(const MCSection *S) {
91     return S->getVariant() == SV_MachO;
92   }
93 };
94 
95 } // end namespace llvm
96 
97 #endif
98