Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:15

0001 //===- MCSectionXCOFF.h - XCOFF Machine Code Sections -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the MCSectionXCOFF class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_MC_MCSECTIONXCOFF_H
0014 #define LLVM_MC_MCSECTIONXCOFF_H
0015 
0016 #include "llvm/BinaryFormat/XCOFF.h"
0017 #include "llvm/MC/MCSection.h"
0018 #include "llvm/MC/MCSymbolXCOFF.h"
0019 
0020 namespace llvm {
0021 
0022 // This class represents an XCOFF `Control Section`, more commonly referred to
0023 // as a csect. A csect represents the smallest possible unit of data/code which
0024 // will be relocated as a single block. A csect can either be:
0025 // 1) Initialized: The Type will be XTY_SD, and the symbols inside the csect
0026 //    will have a label definition representing their offset within the csect.
0027 // 2) Uninitialized: The Type will be XTY_CM, it will contain a single symbol,
0028 //    and may not contain label definitions.
0029 // 3) An external reference providing a symbol table entry for a symbol
0030 //    contained in another XCOFF object file. External reference csects are not
0031 //    implemented yet.
0032 class MCSectionXCOFF final : public MCSection {
0033   friend class MCContext;
0034 
0035   std::optional<XCOFF::CsectProperties> CsectProp;
0036   MCSymbolXCOFF *const QualName;
0037   StringRef SymbolTableName;
0038   std::optional<XCOFF::DwarfSectionSubtypeFlags> DwarfSubtypeFlags;
0039   bool MultiSymbolsAllowed;
0040   SectionKind Kind;
0041   static constexpr unsigned DefaultAlignVal = 4;
0042   static constexpr unsigned DefaultTextAlignVal = 32;
0043 
0044   // XTY_CM sections are virtual except for toc-data symbols.
0045   MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
0046                  XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
0047                  MCSymbol *Begin, StringRef SymbolTableName,
0048                  bool MultiSymbolsAllowed)
0049       : MCSection(SV_XCOFF, Name, K.isText(),
0050                   /*IsVirtual=*/ST == XCOFF::XTY_CM && SMC != XCOFF::XMC_TD,
0051                   Begin),
0052         CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
0053         SymbolTableName(SymbolTableName), DwarfSubtypeFlags(std::nullopt),
0054         MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
0055     assert(
0056         (ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
0057         "Invalid or unhandled type for csect.");
0058     assert(QualName != nullptr && "QualName is needed.");
0059     if (SMC == XCOFF::XMC_UL)
0060       assert((ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
0061              "Invalid csect type for storage mapping class XCOFF::XMC_UL");
0062 
0063     QualName->setRepresentedCsect(this);
0064     QualName->setStorageClass(XCOFF::C_HIDEXT);
0065     if (ST != XCOFF::XTY_ER) {
0066       // For a csect for program code, set the alignment to 32 bytes by default.
0067       // For other csects, set the alignment to 4 bytes by default.
0068       if (SMC == XCOFF::XMC_PR)
0069         setAlignment(Align(DefaultTextAlignVal));
0070       else
0071         setAlignment(Align(DefaultAlignVal));
0072     }
0073   }
0074 
0075   // DWARF sections are never virtual.
0076   MCSectionXCOFF(StringRef Name, SectionKind K, MCSymbolXCOFF *QualName,
0077                  XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
0078                  MCSymbol *Begin, StringRef SymbolTableName,
0079                  bool MultiSymbolsAllowed)
0080       : MCSection(SV_XCOFF, Name, K.isText(), /*IsVirtual=*/false, Begin),
0081         QualName(QualName), SymbolTableName(SymbolTableName),
0082         DwarfSubtypeFlags(DwarfSubtypeFlags),
0083         MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
0084     assert(QualName != nullptr && "QualName is needed.");
0085 
0086     // FIXME: use a more meaningful name for non csect sections.
0087     QualName->setRepresentedCsect(this);
0088 
0089     // Use default text alignment as the alignment for DWARF sections.
0090     setAlignment(Align(DefaultTextAlignVal));
0091   }
0092 
0093   void printCsectDirective(raw_ostream &OS) const;
0094 
0095 public:
0096   ~MCSectionXCOFF();
0097 
0098   static bool classof(const MCSection *S) {
0099     return S->getVariant() == SV_XCOFF;
0100   }
0101 
0102   XCOFF::StorageMappingClass getMappingClass() const {
0103     assert(isCsect() && "Only csect section has mapping class property!");
0104     return CsectProp->MappingClass;
0105   }
0106   XCOFF::StorageClass getStorageClass() const {
0107     return QualName->getStorageClass();
0108   }
0109   XCOFF::VisibilityType getVisibilityType() const {
0110     return QualName->getVisibilityType();
0111   }
0112   XCOFF::SymbolType getCSectType() const {
0113     assert(isCsect() && "Only csect section has symbol type property!");
0114     return CsectProp->Type;
0115   }
0116   MCSymbolXCOFF *getQualNameSymbol() const { return QualName; }
0117 
0118   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
0119                             raw_ostream &OS,
0120                             uint32_t Subsection) const override;
0121   bool useCodeAlign() const override;
0122   StringRef getSymbolTableName() const { return SymbolTableName; }
0123   void setSymbolTableName(StringRef STN) { SymbolTableName = STN; }
0124   bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
0125   bool isCsect() const { return CsectProp.has_value(); }
0126   bool isDwarfSect() const { return DwarfSubtypeFlags.has_value(); }
0127   std::optional<XCOFF::DwarfSectionSubtypeFlags> getDwarfSubtypeFlags() const {
0128     return DwarfSubtypeFlags;
0129   }
0130   std::optional<XCOFF::CsectProperties> getCsectProp() const {
0131     return CsectProp;
0132   }
0133   SectionKind getKind() const { return Kind; }
0134 };
0135 
0136 } // end namespace llvm
0137 
0138 #endif