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/IR/Module.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 Mangler;
27 class MCContext;
28 class MCSection;
29 class MCSymbol;
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   const TargetMachine *TM = nullptr;
40 
41 public:
42   TargetLoweringObjectFileELF() = default;
43   ~TargetLoweringObjectFileELF() override = default;
44 
45   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
46 
47   /// Emit Obj-C garbage collection and linker options.
48   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
49 
50   void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
51                             const MCSymbol *Sym) const override;
52 
53   /// Given a constant with the SectionKind, return a section that it should be
54   /// placed in.
55   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
56                                    const Constant *C,
57                                    unsigned &Align) const override;
58 
59   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
60                                       const TargetMachine &TM) const override;
61 
62   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
63                                     const TargetMachine &TM) const override;
64 
65   MCSection *getSectionForJumpTable(const Function &F,
66                                     const TargetMachine &TM) const override;
67 
68   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
69                                            const Function &F) const override;
70 
71   /// Return an MCExpr to use for a reference to the specified type info global
72   /// variable from exception handling information.
73   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
74                                         unsigned Encoding,
75                                         const TargetMachine &TM,
76                                         MachineModuleInfo *MMI,
77                                         MCStreamer &Streamer) const override;
78 
79   // The symbol that gets passed to .cfi_personality.
80   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
81                                     const TargetMachine &TM,
82                                     MachineModuleInfo *MMI) const override;
83 
84   void InitializeELF(bool UseInitArray_);
85   MCSection *getStaticCtorSection(unsigned Priority,
86                                   const MCSymbol *KeySym) const override;
87   MCSection *getStaticDtorSection(unsigned Priority,
88                                   const MCSymbol *KeySym) const override;
89 
90   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
91                                        const GlobalValue *RHS,
92                                        const TargetMachine &TM) const override;
93 
94   MCSection *getSectionForCommandLines() const override;
95 };
96 
97 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
98 public:
99   TargetLoweringObjectFileMachO();
100   ~TargetLoweringObjectFileMachO() override = default;
101 
102   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
103 
104   /// Emit the module flags that specify the garbage collection information.
105   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
106 
107   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
108                                     const TargetMachine &TM) const override;
109 
110   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
111                                       const TargetMachine &TM) const override;
112 
113   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
114                                    const Constant *C,
115                                    unsigned &Align) const override;
116 
117   /// The mach-o version of this method defaults to returning a stub reference.
118   const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
119                                         unsigned Encoding,
120                                         const TargetMachine &TM,
121                                         MachineModuleInfo *MMI,
122                                         MCStreamer &Streamer) const override;
123 
124   // The symbol that gets passed to .cfi_personality.
125   MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
126                                     const TargetMachine &TM,
127                                     MachineModuleInfo *MMI) const override;
128 
129   /// Get MachO PC relative GOT entry relocation
130   const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
131                                           const MCSymbol *Sym,
132                                           const MCValue &MV, int64_t Offset,
133                                           MachineModuleInfo *MMI,
134                                           MCStreamer &Streamer) const override;
135 
136   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
137                          const TargetMachine &TM) const override;
138 };
139 
140 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
141   mutable unsigned NextUniqueID = 0;
142 
143 public:
144   ~TargetLoweringObjectFileCOFF() override = default;
145 
146   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
147   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
148                                       const TargetMachine &TM) const override;
149 
150   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
151                                     const TargetMachine &TM) const override;
152 
153   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
154                          const TargetMachine &TM) const override;
155 
156   MCSection *getSectionForJumpTable(const Function &F,
157                                     const TargetMachine &TM) const override;
158 
159   /// Emit Obj-C garbage collection and linker options.
160   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
161 
162   MCSection *getStaticCtorSection(unsigned Priority,
163                                   const MCSymbol *KeySym) const override;
164   MCSection *getStaticDtorSection(unsigned Priority,
165                                   const MCSymbol *KeySym) const override;
166 
167   void emitLinkerFlagsForGlobal(raw_ostream &OS,
168                                 const GlobalValue *GV) const override;
169 
170   void emitLinkerFlagsForUsed(raw_ostream &OS,
171                               const GlobalValue *GV) const override;
172 
173   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
174                                        const GlobalValue *RHS,
175                                        const TargetMachine &TM) const override;
176 
177   /// Given a mergeable constant with the specified size and relocation
178   /// information, return a section that it should be placed in.
179   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
180                                    const Constant *C,
181                                    unsigned &Align) const override;
182 };
183 
184 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
185   mutable unsigned NextUniqueID = 0;
186 
187 public:
188   TargetLoweringObjectFileWasm() = default;
189   ~TargetLoweringObjectFileWasm() override = default;
190 
191   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
192                                       const TargetMachine &TM) const override;
193 
194   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
195                                     const TargetMachine &TM) const override;
196 
197   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
198                                            const Function &F) const override;
199 
200   void InitializeWasm();
201   MCSection *getStaticCtorSection(unsigned Priority,
202                                   const MCSymbol *KeySym) const override;
203   MCSection *getStaticDtorSection(unsigned Priority,
204                                   const MCSymbol *KeySym) const override;
205 
206   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
207                                        const GlobalValue *RHS,
208                                        const TargetMachine &TM) const override;
209 };
210 
211 class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
212 public:
213   TargetLoweringObjectFileXCOFF() = default;
214   ~TargetLoweringObjectFileXCOFF() override = default;
215 
216   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
217 
218   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
219                                            const Function &F) const override;
220 
221   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
222                                       const TargetMachine &TM) const override;
223 
224   MCSection *getStaticCtorSection(unsigned Priority,
225                                   const MCSymbol *KeySym) const override;
226   MCSection *getStaticDtorSection(unsigned Priority,
227                                   const MCSymbol *KeySym) const override;
228 
229   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
230                                        const GlobalValue *RHS,
231                                        const TargetMachine &TM) const override;
232 
233   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
234                                     const TargetMachine &TM) const override;
235 
236   MCSection *getSectionForJumpTable(const Function &F,
237                                     const TargetMachine &TM) const override;
238 
239   /// Given a constant with the SectionKind, return a section that it should be
240   /// placed in.
241   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
242                                    const Constant *C,
243                                    unsigned &Align) const override;
244 
245   static XCOFF::StorageClass getStorageClassForGlobal(const GlobalObject *GO);
246 };
247 
248 } // end namespace llvm
249 
250 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
251