1 //==- llvm/CodeGen/TargetLoweringObjectFileImpl.h - Object Info --*- 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 implements classes used to handle lowerings specific to common
10 // object file formats.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
15 #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
16 
17 #include "llvm/IR/Module.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/Target/TargetLoweringObjectFile.h"
20 
21 namespace llvm {
22 
23 class GlobalValue;
24 class MachineModuleInfo;
25 class Mangler;
26 class MCContext;
27 class MCSection;
28 class MCSymbol;
29 class TargetMachine;
30 
31 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
32   bool UseInitArray = false;
33   mutable unsigned NextUniqueID = 1;  // ID 0 is reserved for execute-only sections
34 
35 protected:
36   MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =
37       MCSymbolRefExpr::VK_None;
38   const TargetMachine *TM;
39 
40 public:
41   TargetLoweringObjectFileELF() = default;
42   ~TargetLoweringObjectFileELF() override = default;
43 
44   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
45 
46   /// Emit Obj-C garbage collection and linker options.
47   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
48 
49   void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
50                             const MCSymbol *Sym) const override;
51 
52   /// Given a constant with the SectionKind, return a section that it should be
53   /// placed in.
54   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
55                                    const Constant *C,
56                                    unsigned &Align) const override;
57 
58   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
59                                       const TargetMachine &TM) const override;
60 
61   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
62                                     const TargetMachine &TM) const override;
63 
64   MCSection *getSectionForJumpTable(const Function &F,
65                                     const TargetMachine &TM) const override;
66 
67   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
68                                            const Function &F) const override;
69 
70   /// Return an MCExpr to use for a reference to the specified type info global
71   /// variable from exception handling information.
72   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
73                                         unsigned Encoding,
74                                         const TargetMachine &TM,
75                                         MachineModuleInfo *MMI,
76                                         MCStreamer &Streamer) const override;
77 
78   // The symbol that gets passed to .cfi_personality.
79   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
80                                     const TargetMachine &TM,
81                                     MachineModuleInfo *MMI) const override;
82 
83   void InitializeELF(bool UseInitArray_);
84   MCSection *getStaticCtorSection(unsigned Priority,
85                                   const MCSymbol *KeySym) const override;
86   MCSection *getStaticDtorSection(unsigned Priority,
87                                   const MCSymbol *KeySym) const override;
88 
89   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
90                                        const GlobalValue *RHS,
91                                        const TargetMachine &TM) const override;
92 
93   MCSection *getSectionForCommandLines() const override;
94 };
95 
96 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
97 public:
98   TargetLoweringObjectFileMachO();
99   ~TargetLoweringObjectFileMachO() override = default;
100 
101   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
102 
103   /// Emit the module flags that specify the garbage collection information.
104   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
105 
106   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
107                                     const TargetMachine &TM) const override;
108 
109   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
110                                       const TargetMachine &TM) const override;
111 
112   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
113                                    const Constant *C,
114                                    unsigned &Align) const override;
115 
116   /// The mach-o version of this method defaults to returning a stub reference.
117   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
118                                         unsigned Encoding,
119                                         const TargetMachine &TM,
120                                         MachineModuleInfo *MMI,
121                                         MCStreamer &Streamer) const override;
122 
123   // The symbol that gets passed to .cfi_personality.
124   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
125                                     const TargetMachine &TM,
126                                     MachineModuleInfo *MMI) const override;
127 
128   /// Get MachO PC relative GOT entry relocation
129   const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
130                                           const MCValue &MV, int64_t Offset,
131                                           MachineModuleInfo *MMI,
132                                           MCStreamer &Streamer) const override;
133 
134   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
135                          const TargetMachine &TM) const override;
136 };
137 
138 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
139   mutable unsigned NextUniqueID = 0;
140 
141 public:
142   ~TargetLoweringObjectFileCOFF() override = default;
143 
144   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
145   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
146                                       const TargetMachine &TM) const override;
147 
148   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
149                                     const TargetMachine &TM) const override;
150 
151   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
152                          const TargetMachine &TM) const override;
153 
154   MCSection *getSectionForJumpTable(const Function &F,
155                                     const TargetMachine &TM) const override;
156 
157   /// Emit Obj-C garbage collection and linker options.
158   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
159 
160   MCSection *getStaticCtorSection(unsigned Priority,
161                                   const MCSymbol *KeySym) const override;
162   MCSection *getStaticDtorSection(unsigned Priority,
163                                   const MCSymbol *KeySym) const override;
164 
165   void emitLinkerFlagsForGlobal(raw_ostream &OS,
166                                 const GlobalValue *GV) const override;
167 
168   void emitLinkerFlagsForUsed(raw_ostream &OS,
169                               const GlobalValue *GV) const override;
170 
171   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
172                                        const GlobalValue *RHS,
173                                        const TargetMachine &TM) const override;
174 
175   /// Given a mergeable constant with the specified size and relocation
176   /// information, return a section that it should be placed in.
177   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
178                                    const Constant *C,
179                                    unsigned &Align) const override;
180 };
181 
182 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
183   mutable unsigned NextUniqueID = 0;
184 
185 public:
186   TargetLoweringObjectFileWasm() = default;
187   ~TargetLoweringObjectFileWasm() override = default;
188 
189   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
190                                       const TargetMachine &TM) const override;
191 
192   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
193                                     const TargetMachine &TM) const override;
194 
195   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
196                                            const Function &F) const override;
197 
198   void InitializeWasm();
199   MCSection *getStaticCtorSection(unsigned Priority,
200                                   const MCSymbol *KeySym) const override;
201   MCSection *getStaticDtorSection(unsigned Priority,
202                                   const MCSymbol *KeySym) const override;
203 
204   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
205                                        const GlobalValue *RHS,
206                                        const TargetMachine &TM) const override;
207 };
208 
209 } // end namespace llvm
210 
211 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
212