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