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   Optional<XCOFF::CsectProperties> CsectProp;
36   MCSymbolXCOFF *const QualName;
37   StringRef SymbolTableName;
38   Optional<XCOFF::DwarfSectionSubtypeFlags> DwarfSubtypeFlags;
39   bool MultiSymbolsAllowed;
40   static constexpr unsigned DefaultAlignVal = 4;
41 
MCSectionXCOFF(StringRef Name,XCOFF::StorageMappingClass SMC,XCOFF::SymbolType ST,SectionKind K,MCSymbolXCOFF * QualName,MCSymbol * Begin,StringRef SymbolTableName,bool MultiSymbolsAllowed)42   MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
43                  XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
44                  MCSymbol *Begin, StringRef SymbolTableName,
45                  bool MultiSymbolsAllowed)
46       : MCSection(SV_XCOFF, Name, K, Begin),
47         CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
48         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(None),
49         MultiSymbolsAllowed(MultiSymbolsAllowed) {
50     assert(
51         (ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
52         "Invalid or unhandled type for csect.");
53     assert(QualName != nullptr && "QualName is needed.");
54     if (SMC == XCOFF::XMC_UL)
55       assert((ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
56              "Invalid csect type for storage mapping class XCOFF::XMC_UL");
57 
58     QualName->setRepresentedCsect(this);
59     QualName->setStorageClass(XCOFF::C_HIDEXT);
60     // A csect is 4 byte aligned by default, except for undefined symbol csects.
61     if (ST != XCOFF::XTY_ER)
62       setAlignment(Align(DefaultAlignVal));
63   }
64 
MCSectionXCOFF(StringRef Name,SectionKind K,MCSymbolXCOFF * QualName,XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,MCSymbol * Begin,StringRef SymbolTableName,bool MultiSymbolsAllowed)65   MCSectionXCOFF(StringRef Name, SectionKind K, MCSymbolXCOFF *QualName,
66                  XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
67                  MCSymbol *Begin, StringRef SymbolTableName,
68                  bool MultiSymbolsAllowed)
69       : MCSection(SV_XCOFF, Name, K, Begin), QualName(QualName),
70         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(DwarfSubtypeFlags),
71         MultiSymbolsAllowed(MultiSymbolsAllowed) {
72     assert(QualName != nullptr && "QualName is needed.");
73 
74     // FIXME: use a more meaningful name for non csect sections.
75     QualName->setRepresentedCsect(this);
76 
77     // Set default alignment 4 for all non csect sections for now.
78     // FIXME: set different alignments according to section types.
79     setAlignment(Align(DefaultAlignVal));
80   }
81 
82   void printCsectDirective(raw_ostream &OS) const;
83 
84 public:
85   ~MCSectionXCOFF();
86 
classof(const MCSection * S)87   static bool classof(const MCSection *S) {
88     return S->getVariant() == SV_XCOFF;
89   }
90 
getMappingClass()91   XCOFF::StorageMappingClass getMappingClass() const {
92     assert(isCsect() && "Only csect section has mapping class property!");
93     return CsectProp->MappingClass;
94   }
getStorageClass()95   XCOFF::StorageClass getStorageClass() const {
96     return QualName->getStorageClass();
97   }
getCSectType()98   XCOFF::SymbolType getCSectType() const {
99     assert(isCsect() && "Only csect section has symbol type property!");
100     return CsectProp->Type;
101   }
getQualNameSymbol()102   MCSymbolXCOFF *getQualNameSymbol() const { return QualName; }
103 
104   void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
105                             raw_ostream &OS,
106                             const MCExpr *Subsection) const override;
107   bool UseCodeAlign() const override;
108   bool isVirtualSection() const override;
getSymbolTableName()109   StringRef getSymbolTableName() const { return SymbolTableName; }
isMultiSymbolsAllowed()110   bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
isCsect()111   bool isCsect() const { return CsectProp.hasValue(); }
isDwarfSect()112   bool isDwarfSect() const { return DwarfSubtypeFlags.hasValue(); }
getDwarfSubtypeFlags()113   Optional<XCOFF::DwarfSectionSubtypeFlags> getDwarfSubtypeFlags() const {
114     return DwarfSubtypeFlags;
115   }
116 };
117 
118 } // end namespace llvm
119 
120 #endif
121