xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/ADT/SmallPtrSet.h"
18 #include "llvm/BinaryFormat/XCOFF.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/Target/TargetLoweringObjectFile.h"
21 
22 namespace llvm {
23 
24 class GlobalValue;
25 class MachineModuleInfo;
26 class MachineFunction;
27 class MCContext;
28 class MCExpr;
29 class MCSection;
30 class MCSymbol;
31 class Module;
32 class TargetMachine;
33 
34 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
35   bool UseInitArray = false;
36   mutable unsigned NextUniqueID = 1;  // ID 0 is reserved for execute-only sections
37   SmallPtrSet<GlobalObject *, 2> Used;
38 
39 protected:
40   MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =
41       MCSymbolRefExpr::VK_None;
42 
43 public:
44   TargetLoweringObjectFileELF();
45   ~TargetLoweringObjectFileELF() override = default;
46 
47   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
48 
49   void getModuleMetadata(Module &M) override;
50 
51   /// Emit Obj-C garbage collection and linker options.
52   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
53 
54   void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
55                             const MCSymbol *Sym) const override;
56 
57   /// Given a constant with the SectionKind, return a section that it should be
58   /// placed in.
59   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
60                                    const Constant *C,
61                                    Align &Alignment) const override;
62 
63   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
64                                       const TargetMachine &TM) const override;
65 
66   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
67                                     const TargetMachine &TM) const override;
68 
69   MCSection *getSectionForJumpTable(const Function &F,
70                                     const TargetMachine &TM) const override;
71   MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
72                                const TargetMachine &TM) const override;
73 
74   MCSection *
75   getSectionForMachineBasicBlock(const Function &F,
76                                  const MachineBasicBlock &MBB,
77                                  const TargetMachine &TM) const override;
78 
79   MCSection *
80   getUniqueSectionForFunction(const Function &F,
81                               const TargetMachine &TM) const override;
82 
83   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
84                                            const Function &F) const override;
85 
86   /// Return an MCExpr to use for a reference to the specified type info global
87   /// variable from exception handling information.
88   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
89                                         unsigned Encoding,
90                                         const TargetMachine &TM,
91                                         MachineModuleInfo *MMI,
92                                         MCStreamer &Streamer) const override;
93 
94   // The symbol that gets passed to .cfi_personality.
95   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
96                                     const TargetMachine &TM,
97                                     MachineModuleInfo *MMI) const override;
98 
99   void InitializeELF(bool UseInitArray_);
100   MCSection *getStaticCtorSection(unsigned Priority,
101                                   const MCSymbol *KeySym) const override;
102   MCSection *getStaticDtorSection(unsigned Priority,
103                                   const MCSymbol *KeySym) const override;
104 
105   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
106                                        const GlobalValue *RHS,
107                                        const TargetMachine &TM) const override;
108 
109   const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
110                                         const TargetMachine &TM) const override;
111 
112   MCSection *getSectionForCommandLines() const override;
113 };
114 
115 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
116 public:
117   TargetLoweringObjectFileMachO();
118   ~TargetLoweringObjectFileMachO() override = default;
119 
120   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
121 
122   MCSection *getStaticDtorSection(unsigned Priority,
123                                   const MCSymbol *KeySym) const override;
124 
125   /// Emit the module flags that specify the garbage collection information.
126   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
127 
128   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
129                                     const TargetMachine &TM) const override;
130 
131   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
132                                       const TargetMachine &TM) const override;
133 
134   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
135                                    const Constant *C,
136                                    Align &Alignment) const override;
137 
138   /// The mach-o version of this method defaults to returning a stub reference.
139   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
140                                         unsigned Encoding,
141                                         const TargetMachine &TM,
142                                         MachineModuleInfo *MMI,
143                                         MCStreamer &Streamer) const override;
144 
145   // The symbol that gets passed to .cfi_personality.
146   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
147                                     const TargetMachine &TM,
148                                     MachineModuleInfo *MMI) const override;
149 
150   /// Get MachO PC relative GOT entry relocation
151   const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
152                                           const MCSymbol *Sym,
153                                           const MCValue &MV, int64_t Offset,
154                                           MachineModuleInfo *MMI,
155                                           MCStreamer &Streamer) const override;
156 
157   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
158                          const TargetMachine &TM) const override;
159 
160   MCSection *getSectionForCommandLines() const override;
161 };
162 
163 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
164   mutable unsigned NextUniqueID = 0;
165   const TargetMachine *TM = nullptr;
166 
167 public:
168   ~TargetLoweringObjectFileCOFF() override = default;
169 
170   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
171   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
172                                       const TargetMachine &TM) const override;
173 
174   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
175                                     const TargetMachine &TM) const override;
176 
177   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
178                          const TargetMachine &TM) const override;
179 
180   MCSection *getSectionForJumpTable(const Function &F,
181                                     const TargetMachine &TM) const override;
182 
183   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
184                                            const Function &F) const override;
185 
186   /// Emit Obj-C garbage collection and linker options.
187   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
188 
189   MCSection *getStaticCtorSection(unsigned Priority,
190                                   const MCSymbol *KeySym) const override;
191   MCSection *getStaticDtorSection(unsigned Priority,
192                                   const MCSymbol *KeySym) const override;
193 
194   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
195                                        const GlobalValue *RHS,
196                                        const TargetMachine &TM) const override;
197 
198   /// Given a mergeable constant with the specified size and relocation
199   /// information, return a section that it should be placed in.
200   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
201                                    const Constant *C,
202                                    Align &Alignment) const override;
203 
204 private:
205   void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const;
206 };
207 
208 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
209   mutable unsigned NextUniqueID = 0;
210   SmallPtrSet<GlobalObject *, 2> Used;
211 
212 public:
213   TargetLoweringObjectFileWasm() = default;
214   ~TargetLoweringObjectFileWasm() override = default;
215 
216   void getModuleMetadata(Module &M) override;
217 
218   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
219                                       const TargetMachine &TM) const override;
220 
221   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
222                                     const TargetMachine &TM) const override;
223 
224   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
225                                            const Function &F) const override;
226 
227   void InitializeWasm();
228   MCSection *getStaticCtorSection(unsigned Priority,
229                                   const MCSymbol *KeySym) const override;
230   MCSection *getStaticDtorSection(unsigned Priority,
231                                   const MCSymbol *KeySym) const override;
232 
233   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
234                                        const GlobalValue *RHS,
235                                        const TargetMachine &TM) const override;
236 };
237 
238 class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
239 public:
240   TargetLoweringObjectFileXCOFF() = default;
241   ~TargetLoweringObjectFileXCOFF() override = default;
242 
243   static bool ShouldEmitEHBlock(const MachineFunction *MF);
244   static bool ShouldSetSSPCanaryBitInTB(const MachineFunction *MF);
245 
246   static MCSymbol *getEHInfoTableSymbol(const MachineFunction *MF);
247 
248   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
249 
250   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
251                                            const Function &F) const override;
252 
253   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
254                                       const TargetMachine &TM) const override;
255 
256   MCSection *getStaticCtorSection(unsigned Priority,
257                                   const MCSymbol *KeySym) const override;
258   MCSection *getStaticDtorSection(unsigned Priority,
259                                   const MCSymbol *KeySym) const override;
260 
261   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
262                                        const GlobalValue *RHS,
263                                        const TargetMachine &TM) const override;
264 
265   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
266                                     const TargetMachine &TM) const override;
267 
268   MCSection *getSectionForJumpTable(const Function &F,
269                                     const TargetMachine &TM) const override;
270 
271   /// Given a constant with the SectionKind, return a section that it should be
272   /// placed in.
273   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
274                                    const Constant *C,
275                                    Align &Alignment) const override;
276 
277   static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV);
278 
279   MCSection *
280   getSectionForFunctionDescriptor(const Function *F,
281                                   const TargetMachine &TM) const override;
282   MCSection *getSectionForTOCEntry(const MCSymbol *Sym,
283                                    const TargetMachine &TM) const override;
284 
285   /// For external functions, this will always return a function descriptor
286   /// csect.
287   MCSection *
288   getSectionForExternalReference(const GlobalObject *GO,
289                                  const TargetMachine &TM) const override;
290 
291   /// For functions, this will always return a function descriptor symbol.
292   MCSymbol *getTargetSymbol(const GlobalValue *GV,
293                             const TargetMachine &TM) const override;
294 
295   MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
296                                         const TargetMachine &TM) const override;
297 
298   /// For functions, this will return the LSDA section. If option
299   /// -ffunction-sections is on, this will return a unique csect with the
300   /// function name appended to .gcc_except_table as a suffix of the LSDA
301   /// section name.
302   MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
303                                const TargetMachine &TM) const override;
304 };
305 
306 class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
307 public:
308   TargetLoweringObjectFileGOFF();
309   ~TargetLoweringObjectFileGOFF() override = default;
310 
311   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
312                                     const TargetMachine &TM) const override;
313   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
314                                       const TargetMachine &TM) const override;
315   MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
316                                const TargetMachine &TM) const override;
317 };
318 
319 } // end namespace llvm
320 
321 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
322