1 //===- MCSectionELF.h - ELF 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 MCSectionELF class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCSECTIONELF_H
14 #define LLVM_MC_MCSECTIONELF_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbolELF.h"
19 #include "llvm/MC/SectionKind.h"
20 
21 namespace llvm {
22 
23 class MCSymbol;
24 
25 /// This represents a section on linux, lots of unix variants and some bare
26 /// metal systems.
27 class MCSectionELF final : public MCSection {
28   /// This is the sh_type field of a section, drawn from the enums below.
29   unsigned Type;
30 
31   /// This is the sh_flags field of a section, drawn from the enums below.
32   unsigned Flags;
33 
34   unsigned UniqueID;
35 
36   /// The size of each entry in this section. This size only makes sense for
37   /// sections that contain fixed-sized entries. If a section does not contain
38   /// fixed-sized entries 'EntrySize' will be 0.
39   unsigned EntrySize;
40 
41   const MCSymbolELF *Group;
42 
43   /// Used by SHF_LINK_ORDER. If non-null, the sh_link field will be set to the
44   /// section header index of the section where LinkedToSym is defined.
45   const MCSymbol *LinkedToSym;
46 
47 private:
48   friend class MCContext;
49 
50   // The storage of Name is owned by MCContext's ELFUniquingMap.
51   MCSectionELF(StringRef Name, unsigned type, unsigned flags, SectionKind K,
52                unsigned entrySize, const MCSymbolELF *group, unsigned UniqueID,
53                MCSymbol *Begin, const MCSymbolELF *LinkedToSym)
54       : MCSection(SV_ELF, Name, K, Begin), Type(type), Flags(flags),
55         UniqueID(UniqueID), EntrySize(entrySize), Group(group),
56         LinkedToSym(LinkedToSym) {
57     if (Group)
58       Group->setIsSignature();
59   }
60 
61   // TODO Delete after we stop supporting generation of GNU-style .zdebug_*
62   // sections.
63   void setSectionName(StringRef Name) { this->Name = Name; }
64 
65 public:
66   /// Decides whether a '.section' directive should be printed before the
67   /// section name
68   bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
69 
70   unsigned getType() const { return Type; }
71   unsigned getFlags() const { return Flags; }
72   unsigned getEntrySize() const { return EntrySize; }
73   void setFlags(unsigned F) { Flags = F; }
74   const MCSymbolELF *getGroup() const { return Group; }
75 
76   void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
77                             raw_ostream &OS,
78                             const MCExpr *Subsection) const override;
79   bool UseCodeAlign() const override;
80   bool isVirtualSection() const override;
81   StringRef getVirtualSectionKind() const override;
82 
83   bool isUnique() const { return UniqueID != NonUniqueID; }
84   unsigned getUniqueID() const { return UniqueID; }
85 
86   const MCSection *getLinkedToSection() const {
87     return &LinkedToSym->getSection();
88   }
89   const MCSymbol *getLinkedToSymbol() const { return LinkedToSym; }
90 
91   static bool classof(const MCSection *S) {
92     return S->getVariant() == SV_ELF;
93   }
94 };
95 
96 } // end namespace llvm
97 
98 #endif // LLVM_MC_MCSECTIONELF_H
99