xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCSectionCOFF.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- MCSectionCOFF.h - COFF 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 MCSectionCOFF class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCSECTIONCOFF_H
14 #define LLVM_MC_MCSECTIONCOFF_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/SectionKind.h"
20 #include <cassert>
21 
22 namespace llvm {
23 
24 class MCSymbol;
25 
26 /// This represents a section on Windows
27 class MCSectionCOFF final : public MCSection {
28   // FIXME: The following fields should not be mutable, but are for now so the
29   // asm parser can honor the .linkonce directive.
30 
31   /// This is the Characteristics field of a section, drawn from the enums
32   /// below.
33   mutable unsigned Characteristics;
34 
35   /// The unique IDs used with the .pdata and .xdata sections created internally
36   /// by the assembler. This ID is used to ensure that for every .text section,
37   /// there is exactly one .pdata and one .xdata section, which is required by
38   /// the Microsoft incremental linker. This data is mutable because this ID is
39   /// not notionally part of the section.
40   mutable unsigned WinCFISectionID = ~0U;
41 
42   /// The COMDAT symbol of this section. Only valid if this is a COMDAT section.
43   /// Two COMDAT sections are merged if they have the same COMDAT symbol.
44   MCSymbol *COMDATSymbol;
45 
46   /// This is the Selection field for the section symbol, if it is a COMDAT
47   /// section (Characteristics & IMAGE_SCN_LNK_COMDAT) != 0
48   mutable int Selection;
49 
50   unsigned UniqueID;
51 
52 private:
53   friend class MCContext;
54   // The storage of Name is owned by MCContext's COFFUniquingMap.
MCSectionCOFF(StringRef Name,unsigned Characteristics,MCSymbol * COMDATSymbol,int Selection,unsigned UniqueID,MCSymbol * Begin)55   MCSectionCOFF(StringRef Name, unsigned Characteristics,
56                 MCSymbol *COMDATSymbol, int Selection, unsigned UniqueID,
57                 MCSymbol *Begin)
58       : MCSection(SV_COFF, Name, Characteristics & COFF::IMAGE_SCN_CNT_CODE,
59                   Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA,
60                   Begin),
61         Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
62         Selection(Selection), UniqueID(UniqueID) {
63     assert((Characteristics & 0x00F00000) == 0 &&
64            "alignment must not be set upon section creation");
65   }
66 
67 public:
68   /// Decides whether a '.section' directive should be printed before the
69   /// section name
70   bool shouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
71 
getCharacteristics()72   unsigned getCharacteristics() const { return Characteristics; }
getCOMDATSymbol()73   MCSymbol *getCOMDATSymbol() const { return COMDATSymbol; }
getSelection()74   int getSelection() const { return Selection; }
75 
76   void setSelection(int Selection) const;
77 
isUnique()78   bool isUnique() const { return UniqueID != NonUniqueID; }
getUniqueID()79   unsigned getUniqueID() const { return UniqueID; }
80 
81   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
82                             raw_ostream &OS,
83                             uint32_t Subsection) const override;
84   bool useCodeAlign() const override;
85   StringRef getVirtualSectionKind() const override;
86 
getOrAssignWinCFISectionID(unsigned * NextID)87   unsigned getOrAssignWinCFISectionID(unsigned *NextID) const {
88     if (WinCFISectionID == ~0U)
89       WinCFISectionID = (*NextID)++;
90     return WinCFISectionID;
91   }
92 
isImplicitlyDiscardable(StringRef Name)93   static bool isImplicitlyDiscardable(StringRef Name) {
94     return Name.starts_with(".debug");
95   }
96 
classof(const MCSection * S)97   static bool classof(const MCSection *S) { return S->getVariant() == SV_COFF; }
98 };
99 
100 } // end namespace llvm
101 
102 #endif // LLVM_MC_MCSECTIONCOFF_H
103