xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCSectionXCOFF.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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   SectionKind Kind;
41   static constexpr unsigned DefaultAlignVal = 4;
42   static constexpr unsigned DefaultTextAlignVal = 32;
43 
44   // XTY_CM sections are virtual except for toc-data symbols.
MCSectionXCOFF(StringRef Name,XCOFF::StorageMappingClass SMC,XCOFF::SymbolType ST,SectionKind K,MCSymbolXCOFF * QualName,MCSymbol * Begin,StringRef SymbolTableName,bool MultiSymbolsAllowed)45   MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
46                  XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
47                  MCSymbol *Begin, StringRef SymbolTableName,
48                  bool MultiSymbolsAllowed)
49       : MCSection(SV_XCOFF, Name, K.isText(),
50                   /*IsVirtual=*/ST == XCOFF::XTY_CM && SMC != XCOFF::XMC_TD,
51                   Begin),
52         CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
53         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(std::nullopt),
54         MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
55     assert(
56         (ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
57         "Invalid or unhandled type for csect.");
58     assert(QualName != nullptr && "QualName is needed.");
59     if (SMC == XCOFF::XMC_UL)
60       assert((ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
61              "Invalid csect type for storage mapping class XCOFF::XMC_UL");
62 
63     QualName->setRepresentedCsect(this);
64     QualName->setStorageClass(XCOFF::C_HIDEXT);
65     if (ST != XCOFF::XTY_ER) {
66       // For a csect for program code, set the alignment to 32 bytes by default.
67       // For other csects, set the alignment to 4 bytes by default.
68       if (SMC == XCOFF::XMC_PR)
69         setAlignment(Align(DefaultTextAlignVal));
70       else
71         setAlignment(Align(DefaultAlignVal));
72     }
73   }
74 
75   // DWARF sections are never virtual.
MCSectionXCOFF(StringRef Name,SectionKind K,MCSymbolXCOFF * QualName,XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,MCSymbol * Begin,StringRef SymbolTableName,bool MultiSymbolsAllowed)76   MCSectionXCOFF(StringRef Name, SectionKind K, MCSymbolXCOFF *QualName,
77                  XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
78                  MCSymbol *Begin, StringRef SymbolTableName,
79                  bool MultiSymbolsAllowed)
80       : MCSection(SV_XCOFF, Name, K.isText(), /*IsVirtual=*/false, Begin),
81         QualName(QualName), SymbolTableName(SymbolTableName),
82         DwarfSubtypeFlags(DwarfSubtypeFlags),
83         MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
84     assert(QualName != nullptr && "QualName is needed.");
85 
86     // FIXME: use a more meaningful name for non csect sections.
87     QualName->setRepresentedCsect(this);
88 
89     // Use default text alignment as the alignment for DWARF sections.
90     setAlignment(Align(DefaultTextAlignVal));
91   }
92 
93   void printCsectDirective(raw_ostream &OS) const;
94 
95 public:
96   ~MCSectionXCOFF();
97 
classof(const MCSection * S)98   static bool classof(const MCSection *S) {
99     return S->getVariant() == SV_XCOFF;
100   }
101 
getMappingClass()102   XCOFF::StorageMappingClass getMappingClass() const {
103     assert(isCsect() && "Only csect section has mapping class property!");
104     return CsectProp->MappingClass;
105   }
getStorageClass()106   XCOFF::StorageClass getStorageClass() const {
107     return QualName->getStorageClass();
108   }
getVisibilityType()109   XCOFF::VisibilityType getVisibilityType() const {
110     return QualName->getVisibilityType();
111   }
getCSectType()112   XCOFF::SymbolType getCSectType() const {
113     assert(isCsect() && "Only csect section has symbol type property!");
114     return CsectProp->Type;
115   }
getQualNameSymbol()116   MCSymbolXCOFF *getQualNameSymbol() const { return QualName; }
117 
118   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
119                             raw_ostream &OS,
120                             uint32_t Subsection) const override;
121   bool useCodeAlign() const override;
getSymbolTableName()122   StringRef getSymbolTableName() const { return SymbolTableName; }
setSymbolTableName(StringRef STN)123   void setSymbolTableName(StringRef STN) { SymbolTableName = STN; }
isMultiSymbolsAllowed()124   bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
isCsect()125   bool isCsect() const { return CsectProp.has_value(); }
isDwarfSect()126   bool isDwarfSect() const { return DwarfSubtypeFlags.has_value(); }
getDwarfSubtypeFlags()127   std::optional<XCOFF::DwarfSectionSubtypeFlags> getDwarfSubtypeFlags() const {
128     return DwarfSubtypeFlags;
129   }
getCsectProp()130   std::optional<XCOFF::CsectProperties> getCsectProp() const {
131     return CsectProp;
132   }
getKind()133   SectionKind getKind() const { return Kind; }
134 };
135 
136 } // end namespace llvm
137 
138 #endif
139