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