1 //===- MCSectionXCOFF.h - XCOFF 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 MCSectionXCOFF class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCSECTIONXCOFF_H
14 #define LLVM_MC_MCSECTIONXCOFF_H
15 
16 #include "llvm/BinaryFormat/XCOFF.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbolXCOFF.h"
19 
20 namespace llvm {
21 
22 // This class represents an XCOFF `Control Section`, more commonly referred to
23 // as a csect. A csect represents the smallest possible unit of data/code which
24 // will be relocated as a single block. A csect can either be:
25 // 1) Initialized: The Type will be XTY_SD, and the symbols inside the csect
26 //    will have a label definition representing their offset within the csect.
27 // 2) Uninitialized: The Type will be XTY_CM, it will contain a single symbol,
28 //    and may not contain label definitions.
29 // 3) An external reference providing a symbol table entry for a symbol
30 //    contained in another XCOFF object file. External reference csects are not
31 //    implemented yet.
32 class MCSectionXCOFF final : public MCSection {
33   friend class MCContext;
34 
35   std::optional<XCOFF::CsectProperties> CsectProp;
36   MCSymbolXCOFF *const QualName;
37   StringRef SymbolTableName;
38   std::optional<XCOFF::DwarfSectionSubtypeFlags> DwarfSubtypeFlags;
39   bool MultiSymbolsAllowed;
40   static constexpr unsigned DefaultAlignVal = 4;
41   static constexpr unsigned DefaultTextAlignVal = 32;
42 
43   MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
44                  XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
45                  MCSymbol *Begin, StringRef SymbolTableName,
46                  bool MultiSymbolsAllowed)
47       : MCSection(SV_XCOFF, Name, K, Begin),
48         CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
49         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(std::nullopt),
50         MultiSymbolsAllowed(MultiSymbolsAllowed) {
51     assert(
52         (ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
53         "Invalid or unhandled type for csect.");
54     assert(QualName != nullptr && "QualName is needed.");
55     if (SMC == XCOFF::XMC_UL)
56       assert((ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
57              "Invalid csect type for storage mapping class XCOFF::XMC_UL");
58 
59     QualName->setRepresentedCsect(this);
60     QualName->setStorageClass(XCOFF::C_HIDEXT);
61     if (ST != XCOFF::XTY_ER) {
62       // For a csect for program code, set the alignment to 32 bytes by default.
63       // For other csects, set the alignment to 4 bytes by default.
64       if (SMC == XCOFF::XMC_PR)
65         setAlignment(Align(DefaultTextAlignVal));
66       else
67         setAlignment(Align(DefaultAlignVal));
68     }
69   }
70 
71   MCSectionXCOFF(StringRef Name, SectionKind K, MCSymbolXCOFF *QualName,
72                  XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
73                  MCSymbol *Begin, StringRef SymbolTableName,
74                  bool MultiSymbolsAllowed)
75       : MCSection(SV_XCOFF, Name, K, Begin), QualName(QualName),
76         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(DwarfSubtypeFlags),
77         MultiSymbolsAllowed(MultiSymbolsAllowed) {
78     assert(QualName != nullptr && "QualName is needed.");
79 
80     // FIXME: use a more meaningful name for non csect sections.
81     QualName->setRepresentedCsect(this);
82 
83     // Use default text alignment as the alignment for DWARF sections.
84     setAlignment(Align(DefaultTextAlignVal));
85   }
86 
87   void printCsectDirective(raw_ostream &OS) const;
88 
89 public:
90   ~MCSectionXCOFF();
91 
92   static bool classof(const MCSection *S) {
93     return S->getVariant() == SV_XCOFF;
94   }
95 
96   XCOFF::StorageMappingClass getMappingClass() const {
97     assert(isCsect() && "Only csect section has mapping class property!");
98     return CsectProp->MappingClass;
99   }
100   XCOFF::StorageClass getStorageClass() const {
101     return QualName->getStorageClass();
102   }
103   XCOFF::VisibilityType getVisibilityType() const {
104     return QualName->getVisibilityType();
105   }
106   XCOFF::SymbolType getCSectType() const {
107     assert(isCsect() && "Only csect section has symbol type property!");
108     return CsectProp->Type;
109   }
110   MCSymbolXCOFF *getQualNameSymbol() const { return QualName; }
111 
112   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
113                             raw_ostream &OS,
114                             const MCExpr *Subsection) const override;
115   bool useCodeAlign() const override;
116   bool isVirtualSection() const override;
117   StringRef getSymbolTableName() const { return SymbolTableName; }
118   bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
119   bool isCsect() const { return CsectProp.has_value(); }
120   bool isDwarfSect() const { return DwarfSubtypeFlags.has_value(); }
121   std::optional<XCOFF::DwarfSectionSubtypeFlags> getDwarfSubtypeFlags() const {
122     return DwarfSubtypeFlags;
123   }
124   std::optional<XCOFF::CsectProperties> getCsectProp() const {
125     return CsectProp;
126   }
127 };
128 
129 } // end namespace llvm
130 
131 #endif
132