xref: /llvm-project/llvm/include/llvm/Target/TargetLoweringObjectFile.h (revision 3feb724496238ce10d32e8c2bd84b4ea50f9977e)
1 //===-- llvm/Target/TargetLoweringObjectFile.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_TARGET_TARGETLOWERINGOBJECTFILE_H
15 #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16 
17 #include "llvm/MC/MCObjectFileInfo.h"
18 #include "llvm/MC/MCRegister.h"
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 struct Align;
24 struct MachineJumpTableEntry;
25 class Constant;
26 class DataLayout;
27 class Function;
28 class GlobalObject;
29 class GlobalValue;
30 class MachineBasicBlock;
31 class MachineModuleInfo;
32 class Mangler;
33 class MCContext;
34 class MCExpr;
35 class MCSection;
36 class MCSymbol;
37 class MCSymbolRefExpr;
38 class MCStreamer;
39 class MCValue;
40 class Module;
41 class SectionKind;
42 class StringRef;
43 class TargetMachine;
44 class DSOLocalEquivalent;
45 
46 class TargetLoweringObjectFile : public MCObjectFileInfo {
47   /// Name-mangler for global names.
48   Mangler *Mang = nullptr;
49 
50 protected:
51   bool SupportIndirectSymViaGOTPCRel = false;
52   bool SupportGOTPCRelWithOffset = true;
53   bool SupportDebugThreadLocalLocation = true;
54   bool SupportDSOLocalEquivalentLowering = false;
55 
56   /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
57   /// for EH.
58   unsigned PersonalityEncoding = 0;
59   unsigned LSDAEncoding = 0;
60   unsigned TTypeEncoding = 0;
61   unsigned CallSiteEncoding = 0;
62 
63   /// This section contains the static constructor pointer list.
64   MCSection *StaticCtorSection = nullptr;
65 
66   /// This section contains the static destructor pointer list.
67   MCSection *StaticDtorSection = nullptr;
68 
69   const TargetMachine *TM = nullptr;
70 
71 public:
72   TargetLoweringObjectFile() = default;
73   TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
74   TargetLoweringObjectFile &
75   operator=(const TargetLoweringObjectFile &) = delete;
76   virtual ~TargetLoweringObjectFile();
77 
78   Mangler &getMangler() const { return *Mang; }
79 
80   /// This method must be called before any actual lowering is done.  This
81   /// specifies the current context for codegen, and gives the lowering
82   /// implementations a chance to set up their default sections.
83   virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
84 
85   virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
86                                     const MCSymbol *Sym,
87                                     const MachineModuleInfo *MMI) const;
88 
89   /// Emit the module-level metadata that the platform cares about.
90   virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
91 
92   /// Emit Call Graph Profile metadata.
93   void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
94 
95   /// Process linker options metadata and emit platform-specific bits.
96   virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const {}
97 
98   /// Get the module-level metadata that the platform cares about.
99   virtual void getModuleMetadata(Module &M) {}
100 
101   /// Given a constant with the SectionKind, return a section that it should be
102   /// placed in.
103   virtual MCSection *getSectionForConstant(const DataLayout &DL,
104                                            SectionKind Kind, const Constant *C,
105                                            Align &Alignment) const;
106 
107   virtual MCSection *
108   getSectionForMachineBasicBlock(const Function &F,
109                                  const MachineBasicBlock &MBB,
110                                  const TargetMachine &TM) const;
111 
112   virtual MCSection *
113   getUniqueSectionForFunction(const Function &F,
114                               const TargetMachine &TM) const;
115 
116   /// Classify the specified global variable into a set of target independent
117   /// categories embodied in SectionKind.
118   static SectionKind getKindForGlobal(const GlobalObject *GO,
119                                       const TargetMachine &TM);
120 
121   /// This method computes the appropriate section to emit the specified global
122   /// variable or function definition. This should not be passed external (or
123   /// available externally) globals.
124   MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
125                               const TargetMachine &TM) const;
126 
127   /// This method computes the appropriate section to emit the specified global
128   /// variable or function definition. This should not be passed external (or
129   /// available externally) globals.
130   MCSection *SectionForGlobal(const GlobalObject *GO,
131                               const TargetMachine &TM) const;
132 
133   virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
134                                  const GlobalValue *GV,
135                                  const TargetMachine &TM) const;
136 
137   virtual MCSection *getSectionForJumpTable(const Function &F,
138                                             const TargetMachine &TM) const;
139   virtual MCSection *
140   getSectionForJumpTable(const Function &F, const TargetMachine &TM,
141                          const MachineJumpTableEntry *JTE) const;
142 
143   virtual MCSection *getSectionForLSDA(const Function &, const MCSymbol &,
144                                        const TargetMachine &) const {
145     return LSDASection;
146   }
147 
148   virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
149                                                    const Function &F) const;
150 
151   /// Targets should implement this method to assign a section to globals with
152   /// an explicit section specfied. The implementation of this method can
153   /// assume that GO->hasSection() is true.
154   virtual MCSection *
155   getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
156                            const TargetMachine &TM) const = 0;
157 
158   /// Return an MCExpr to use for a reference to the specified global variable
159   /// from exception handling information.
160   virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
161                                                 unsigned Encoding,
162                                                 const TargetMachine &TM,
163                                                 MachineModuleInfo *MMI,
164                                                 MCStreamer &Streamer) const;
165 
166   /// Return the MCSymbol for a private symbol with global value name as its
167   /// base, with the specified suffix.
168   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
169                                          StringRef Suffix,
170                                          const TargetMachine &TM) const;
171 
172   // The symbol that gets passed to .cfi_personality.
173   virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
174                                             const TargetMachine &TM,
175                                             MachineModuleInfo *MMI) const;
176 
177   unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
178   unsigned getLSDAEncoding() const { return LSDAEncoding; }
179   unsigned getTTypeEncoding() const { return TTypeEncoding; }
180   unsigned getCallSiteEncoding() const;
181 
182   const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
183                                   MCStreamer &Streamer) const;
184 
185   virtual MCSection *getStaticCtorSection(unsigned Priority,
186                                           const MCSymbol *KeySym) const {
187     return StaticCtorSection;
188   }
189 
190   virtual MCSection *getStaticDtorSection(unsigned Priority,
191                                           const MCSymbol *KeySym) const {
192     return StaticDtorSection;
193   }
194 
195   /// Create a symbol reference to describe the given TLS variable when
196   /// emitting the address in debug info.
197   virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
198 
199   virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
200                                                const GlobalValue *RHS,
201                                                const TargetMachine &TM) const {
202     return nullptr;
203   }
204 
205   /// Target supports a native lowering of a dso_local_equivalent constant
206   /// without needing to replace it with equivalent IR.
207   bool supportDSOLocalEquivalentLowering() const {
208     return SupportDSOLocalEquivalentLowering;
209   }
210 
211   virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
212                                                 const TargetMachine &TM) const {
213     return nullptr;
214   }
215 
216   /// Target supports replacing a data "PC"-relative access to a symbol
217   /// through another symbol, by accessing the later via a GOT entry instead?
218   bool supportIndirectSymViaGOTPCRel() const {
219     return SupportIndirectSymViaGOTPCRel;
220   }
221 
222   /// Target GOT "PC"-relative relocation supports encoding an additional
223   /// binary expression with an offset?
224   bool supportGOTPCRelWithOffset() const {
225     return SupportGOTPCRelWithOffset;
226   }
227 
228   /// Target supports TLS offset relocation in debug section?
229   bool supportDebugThreadLocalLocation() const {
230     return SupportDebugThreadLocalLocation;
231   }
232 
233   /// Returns the register used as static base in RWPI variants.
234   virtual MCRegister getStaticBase() const { return MCRegister::NoRegister; }
235 
236   /// Get the target specific RWPI relocation.
237   virtual const MCExpr *getIndirectSymViaRWPI(const MCSymbol *Sym) const {
238     return nullptr;
239   }
240 
241   /// Get the target specific PC relative GOT entry relocation
242   virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
243                                                   const MCSymbol *Sym,
244                                                   const MCValue &MV,
245                                                   int64_t Offset,
246                                                   MachineModuleInfo *MMI,
247                                                   MCStreamer &Streamer) const {
248     return nullptr;
249   }
250 
251   /// If supported, return the section to use for the llvm.commandline
252   /// metadata. Otherwise, return nullptr.
253   virtual MCSection *getSectionForCommandLines() const {
254     return nullptr;
255   }
256 
257   /// On targets that use separate function descriptor symbols, return a section
258   /// for the descriptor given its symbol. Use only with defined functions.
259   virtual MCSection *
260   getSectionForFunctionDescriptor(const Function *F,
261                                   const TargetMachine &TM) const {
262     return nullptr;
263   }
264 
265   /// On targets that support TOC entries, return a section for the entry given
266   /// the symbol it refers to.
267   /// TODO: Implement this interface for existing ELF targets.
268   virtual MCSection *getSectionForTOCEntry(const MCSymbol *S,
269                                            const TargetMachine &TM) const {
270     return nullptr;
271   }
272 
273   /// On targets that associate external references with a section, return such
274   /// a section for the given external global.
275   virtual MCSection *
276   getSectionForExternalReference(const GlobalObject *GO,
277                                  const TargetMachine &TM) const {
278     return nullptr;
279   }
280 
281   /// Targets that have a special convention for their symbols could use
282   /// this hook to return a specialized symbol.
283   virtual MCSymbol *getTargetSymbol(const GlobalValue *GV,
284                                     const TargetMachine &TM) const {
285     return nullptr;
286   }
287 
288   /// If supported, return the function entry point symbol.
289   /// Otherwise, returns nullptr.
290   /// Func must be a function or an alias which has a function as base object.
291   virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
292                                                 const TargetMachine &TM) const {
293     return nullptr;
294   }
295 
296 protected:
297   virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
298                                             SectionKind Kind,
299                                             const TargetMachine &TM) const = 0;
300 };
301 
302 } // end namespace llvm
303 
304 #endif // LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
305