1 //==- llvm/CodeGen/TargetLoweringObjectFileImpl.h - Object Info --*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
16 #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
17 
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;
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 MCSymbol *Sym,
131                                           const MCValue &MV, int64_t Offset,
132                                           MachineModuleInfo *MMI,
133                                           MCStreamer &Streamer) const override;
134 
135   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
136                          const TargetMachine &TM) const override;
137 };
138 
139 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
140   mutable unsigned NextUniqueID = 0;
141 
142 public:
143   ~TargetLoweringObjectFileCOFF() override = default;
144 
145   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
146   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
147                                       const TargetMachine &TM) const override;
148 
149   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
150                                     const TargetMachine &TM) const override;
151 
152   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
153                          const TargetMachine &TM) const override;
154 
155   MCSection *getSectionForJumpTable(const Function &F,
156                                     const TargetMachine &TM) const override;
157 
158   /// Emit Obj-C garbage collection and linker options.
159   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
160 
161   MCSection *getStaticCtorSection(unsigned Priority,
162                                   const MCSymbol *KeySym) const override;
163   MCSection *getStaticDtorSection(unsigned Priority,
164                                   const MCSymbol *KeySym) const override;
165 
166   void emitLinkerFlagsForGlobal(raw_ostream &OS,
167                                 const GlobalValue *GV) const override;
168 
169   void emitLinkerFlagsForUsed(raw_ostream &OS,
170                               const GlobalValue *GV) const override;
171 
172   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
173                                        const GlobalValue *RHS,
174                                        const TargetMachine &TM) const override;
175 
176   /// Given a mergeable constant with the specified size and relocation
177   /// information, return a section that it should be placed in.
178   MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
179                                    const Constant *C,
180                                    unsigned &Align) const override;
181 };
182 
183 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
184   mutable unsigned NextUniqueID = 0;
185 
186 public:
187   TargetLoweringObjectFileWasm() = default;
188   ~TargetLoweringObjectFileWasm() override = default;
189 
190   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
191                                       const TargetMachine &TM) const override;
192 
193   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
194                                     const TargetMachine &TM) const override;
195 
196   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
197                                            const Function &F) const override;
198 
199   void InitializeWasm();
200   MCSection *getStaticCtorSection(unsigned Priority,
201                                   const MCSymbol *KeySym) const override;
202   MCSection *getStaticDtorSection(unsigned Priority,
203                                   const MCSymbol *KeySym) const override;
204 
205   const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
206                                        const GlobalValue *RHS,
207                                        const TargetMachine &TM) const override;
208 };
209 
210 } // end namespace llvm
211 
212 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
213