xref: /llvm-project/llvm/include/llvm/MC/MCSectionELF.h (revision 183ddefff60f560466ec3bf271c6008067d746db)
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/PointerIntPair.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSymbolELF.h"
21 #include "llvm/MC/SectionKind.h"
22 
23 namespace llvm {
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   /// The section group signature symbol (if not null) and a bool indicating
42   /// whether this is a GRP_COMDAT group.
43   const PointerIntPair<const MCSymbolELF *, 1, bool> Group;
44 
45   /// Used by SHF_LINK_ORDER. If non-null, the sh_link field will be set to the
46   /// section header index of the section where LinkedToSym is defined.
47   const MCSymbol *LinkedToSym;
48 
49   /// Start/end offset in file, used by ELFWriter.
50   uint64_t StartOffset;
51   uint64_t EndOffset;
52 
53 private:
54   friend class MCContext;
55 
56   // The storage of Name is owned by MCContext's ELFUniquingMap.
57   MCSectionELF(StringRef Name, unsigned type, unsigned flags,
58                unsigned entrySize, const MCSymbolELF *group, bool IsComdat,
59                unsigned UniqueID, MCSymbol *Begin,
60                const MCSymbolELF *LinkedToSym)
61       : MCSection(SV_ELF, Name, flags & ELF::SHF_EXECINSTR,
62                   type == ELF::SHT_NOBITS, Begin),
63         Type(type), Flags(flags), UniqueID(UniqueID), EntrySize(entrySize),
64         Group(group, IsComdat), LinkedToSym(LinkedToSym) {
65     assert((!(Flags & ELF::SHF_GROUP) || Group.getPointer()) &&
66            "Group section without signature!");
67     if (Group.getPointer())
68       Group.getPointer()->setIsSignature();
69   }
70 
71   // TODO Delete after we stop supporting generation of GNU-style .zdebug_*
72   // sections.
73   void setSectionName(StringRef Name) { this->Name = Name; }
74 
75 public:
76   /// Decides whether a '.section' directive should be printed before the
77   /// section name
78   bool shouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
79 
80   unsigned getType() const { return Type; }
81   unsigned getFlags() const { return Flags; }
82   unsigned getEntrySize() const { return EntrySize; }
83   void setFlags(unsigned F) { Flags = F; }
84   const MCSymbolELF *getGroup() const { return Group.getPointer(); }
85   bool isComdat() const { return Group.getInt(); }
86 
87   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
88                             raw_ostream &OS,
89                             uint32_t Subsection) const override;
90   bool useCodeAlign() const override;
91   StringRef getVirtualSectionKind() const override;
92 
93   bool isUnique() const { return UniqueID != NonUniqueID; }
94   unsigned getUniqueID() const { return UniqueID; }
95 
96   const MCSection *getLinkedToSection() const {
97     return &LinkedToSym->getSection();
98   }
99   const MCSymbol *getLinkedToSymbol() const { return LinkedToSym; }
100 
101   void setOffsets(uint64_t Start, uint64_t End) {
102     StartOffset = Start;
103     EndOffset = End;
104   }
105   std::pair<uint64_t, uint64_t> getOffsets() const {
106     return std::make_pair(StartOffset, EndOffset);
107   }
108 
109   static bool classof(const MCSection *S) {
110     return S->getVariant() == SV_ELF;
111   }
112 };
113 
114 } // end namespace llvm
115 
116 #endif // LLVM_MC_MCSECTIONELF_H
117